Ejemplo n.º 1
0
        //[ValidateAntiForgeryToken]
        public async Task <ActionResult <string> > Save([FromBody] Invoices invoice)
        {
            try
            {
                if (ModelState.IsValid)  //if (invoices != null)
                {
                    // set ip & user id
                    var exitsBefore = await _context.Invoices.FindAsync(invoice.Id);

                    if (exitsBefore != null)
                    { // update
                        exitsBefore.InvoiceNo     = invoice.InvoiceNo;
                        exitsBefore.Notes         = invoice.Notes;
                        exitsBefore.NetAmount     = invoice.NetAmount;
                        exitsBefore.Discount      = invoice.Discount;
                        exitsBefore.TotalAmount   = invoice.TotalAmount;
                        exitsBefore.AccountId     = invoice.AccountId;
                        exitsBefore.InvoiceTypeId = invoice.InvoiceTypeId;
                        exitsBefore.AddDate       = invoice.AddDate;
                        _context.Update(exitsBefore);
                        _context.UpdateRange(invoice.InvoiceDetails);
                    }
                    else // insert
                    {
                        _context.Add(invoice);
                        foreach (var itm in invoice.InvoiceDetails)
                        {
                            itm.InvoiceId = invoice.Id;
                        }
                        _context.AddRange(invoice.InvoiceDetails);
                    }

                    await _context.SaveChangesAsync();

                    return(CreatedAtAction("Result", new { id = invoice.Id, saved = true }));
                }
                else
                {
                    return(CreatedAtAction("Result", new { id = 0, saved = false }));
                }
            }
            catch (Exception ex) { return(CreatedAtAction("Result", new { id = ex.Message, saved = false })); }
        }