示例#1
0
        public async Task <Invoice> Add(Invoice invoice)
        {
            _InvoiceContext.Add(invoice); //Change Tracker : only change the state
            await _InvoiceContext.SaveChangesAsync();

            return(invoice);
        }
示例#2
0
        public async Task <User> Insert(User user)
        {
            user.Password = _encryptor.Encrypt(user.Password);
            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return(user);
        }
示例#3
0
        public async Task <IActionResult> Create([Bind("InvoiceNumber,Amount,CostCategory,Period")] Invoice invoice)
        {
            if (ModelState.IsValid)
            {
                _context.Add(invoice);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(invoice));
        }
        public async Task <IActionResult> PayInvoice(int id)
        {
            var invoice = await _context.Invoices.Include(x => x.Items).FirstOrDefaultAsync(m => m.Id == id);

            if (invoice == null)
            {
                return(NotFound());
            }
            if (invoice.Paid)
            {
                return(BadRequest());
            }
            invoice.Paid = true;
            _context.Update(invoice);
            await _context.SaveChangesAsync();

            return(Ok());
        }
 public async Task Save()
 {
     try
     {
         await _context.SaveChangesAsync();
     }
     catch (DbUpdateException ex)
     {
         throw new RepositoryException("Save Fail", ex);
     }
 }
        public async Task <CommandResult> Handle(CreateInvoice notification, CancellationToken cancellationToken)
        {
            var invoice = notification.ToInVoice();

            if (context.Invoices.Any(_ => _.ID == invoice.ID))
            {
                return(new CommandResult(true, invoice.ID, "ERR-1001", "ID already exists"));
            }
            if (context.Invoices.Any(_ => _.UserId == invoice.UserId && _.Date == invoice.Date))
            {
                return(new CommandResult(true, invoice.ID, "ERR-1002", "Invoice Entry already exists"));
            }
            context.EventEntries.Add(new EventEntry()
            {
                ID = Guid.NewGuid(), Payload = JsonSerializer.Serialize(notification)
            });

            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    await context.Invoices.AddAsync(invoice, cancellationToken);

                    await context.SaveChangesAsync(cancellationToken);

                    await transaction.CommitAsync(cancellationToken);

                    // publish creation to subscribers
                    await mediator.Publish(new InvoiceCreatedEvent(invoice.ID, invoice.Date), cancellationToken);

                    return(new CommandResult(true, invoice.ID));
                }
                catch (Exception e)
                {
                    await transaction.RollbackAsync(cancellationToken);

                    return(new CommandResult(false, Guid.Empty, "ERR-2000", e.Message));
                }
            }
        }
        // // PATCH: api/Invoices/5

        public async Task <IHttpActionResult> PatchInvoice(int id, Invoice invoice)
        {
            if (id != invoice.InvoiceId)
            {
                return(BadRequest());
            }
            if (!InvoiceExists(id))
            {
                return(NotFound());
            }

            if (invoice.Status == null)
            {
                return(BadRequest());
            }
            Invoice original = await db.Invoices.FindAsync(id);

            original.Status     = invoice.Status;
            original.StatusDate = DateTime.Now;
            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (!InvoiceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(Ok(original));
        }
示例#8
0
        public async Task <InvoiceView> CreateInvoice(Models.CreateInvoice createInvoice)
        {
            try
            {
                var result = new InvoiceView();
                using (var db = new InvoiceContext())
                {
                    var invoice = new Database.Invoice();
                    db.Invoices.Add(invoice);
                    invoice.CreatedDate        = createInvoice.CreatedDate;
                    invoice.DueDate            = createInvoice.DueDate;
                    invoice.EmailTo            = createInvoice.EmailTo;
                    invoice.OrderedBy          = createInvoice.OrderedBy;
                    invoice.PaymentDetails     = createInvoice.PaymentDetails;
                    invoice.PurchaseOrderRef   = createInvoice.PurchaseOrderRef;
                    invoice.TermsAndConditions = createInvoice.TermsAndConditions;
                    await db.SaveChangesAsync();

                    result.CreatedDate      = invoice.CreatedDate;
                    result.DueDate          = invoice.DueDate;
                    result.EmailTo          = invoice.EmailTo;
                    result.GrandTotal       = invoice.GrandTotal;
                    result.InvoiceEmailed   = invoice.InvoiceEmailed;
                    result.InvoiceId        = invoice.InvoiceId;
                    result.InvoiceNo        = invoice.InvoiceNo;
                    result.OrderedBy        = invoice.OrderedBy;
                    result.PaidAmount       = invoice.PaidAmount;
                    result.PaidDate         = invoice.PaidDate;
                    result.PaidTax          = invoice.PaidTax;
                    result.PaymentDetails   = invoice.PaymentDetails;
                    result.PurchaseOrderRef = invoice.PurchaseOrderRef;
                    result.SubTotal         = invoice.SubTotal;
                    result.Tax = invoice.Tax;
                    result.TermsAndConditions = invoice.TermsAndConditions;
                    return(result);
                }
            }
            catch (Exception e)
            {
                LogFactory.GetLogger().Log(LogLevel.Error, e);
                return(null);
            }
        }
示例#9
0
        public async Task GenerateDocuments(List <InvoiceLine> lines, TraceWriter log)
        {
            Random random = new Random();

            using (var context = new InvoiceContext())
            {
                await context.Database.EnsureCreatedAsync();

                log.Info("Database is available!");
                foreach (InvoiceLine line in lines)
                {
                    int id = random.Next(10000);
                    context.InvoiceLines.Add(new InvoiceLine {
                        Id = id, Invoice = line.Invoice, Item = line.Item, Amount = line.Amount, Price = line.Price, Qty = line.Qty
                    });
                    var changeId = await context.SaveChangesAsync();

                    log.Info("Invoice lines are added " + changeId);
                }
            }
        }