public async Task <IActionResult> Edit(int id, ProfitPierdere profitPierdere)
        {
            if (id != profitPierdere.ProfitPierdereId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.ProfitPierdere.Update(profitPierdere);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProfitPierdereExists(profitPierdere.ProfitPierdereId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                TempData["Message"] = "Solduri profit pierdere actualizate cu succes!";
                TempData["Success"] = "true";
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ClientId"] = new SelectList(_context.Client.OrderBy(x => x.Denumire), "ClientId", "Denumire", profitPierdere.ClientId);
            return(View(profitPierdere));
        }
        private static ProfitPierdere CreateProfitPierdere(XElement profitP, ImportBalanteXMLVM balanta)
        {
            ProfitPierdere profitPierdere = new ProfitPierdere()
            {
                deb_prec  = float.Parse(profitP.Element("deb_prec").Value),
                cred_prec = float.Parse(profitP.Element("cred_prec").Value),
                rulaj_d   = float.Parse(profitP.Element("rulaj_d").Value),
                rulaj_c   = float.Parse(profitP.Element("rulaj_c").Value),
                fin_d     = float.Parse(profitP.Element("fin_d").Value),
                fin_c     = float.Parse(profitP.Element("fin_c").Value),
                ClientId  = balanta.ClientId,
                Year      = balanta.Year,
                Month     = balanta.Month
            };

            // daca veniturile sunt mai mari atunci avem profit
            if (profitPierdere.rulaj_c > profitPierdere.rulaj_d)
            {
                profitPierdere.Profit_luna = profitPierdere.rulaj_c - profitPierdere.rulaj_d;
            }
            // altfel avem pierdere
            else
            {
                profitPierdere.Pierdere_luna = profitPierdere.rulaj_c - profitPierdere.rulaj_d;
            }

            return(profitPierdere);
        }
        public async Task <IActionResult> ManualCreate(ProfitPierdere profitPierdere)
        {
            if (ModelState.IsValid)
            {
                _context.ProfitPierdere.Add(profitPierdere);
                await _context.SaveChangesAsync();

                TempData["Message"] = "Solduri profit pierdere adaugate cu succes!";
                TempData["Success"] = "true";
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ClientId"] = new SelectList(_context.Client.OrderBy(x => x.Denumire), "ClientId", "Denumire", profitPierdere.ClientId);
            return(View(profitPierdere));
        }
        public async Task <IActionResult> Create(ImportBalanteXMLVM balanta)
        {
            var user = await _userManager.GetUserAsync(User);

            var documentType = _context.TipDocument.FirstOrDefault(u => u.Denumire == "XML").TipDocumentId;

            if (Path.GetExtension(balanta.DocumentPathUrl.FileName).ToLower() != ".xml")
            {
                // if we don't have a xml document
                TempData["Message"]  = "Va rugam incarcati un document in format XML!";
                TempData["Success"]  = "";
                ViewData["ClientId"] = new SelectList(_context.Client.OrderBy(u => u.Denumire), "ClientId", "Denumire", balanta.ClientId);
                return(View(balanta));
            }

            Document document = new Document()
            {
                ApplicationUserId = user.Id,
                ClientId          = balanta.ClientId,
                TipDocumentId     = documentType,
                DocumentPath      = await _fileManager.SaveDocument(balanta.DocumentPathUrl, "XML", balanta.ClientId, user.Id),
            };

            if (ModelState.IsValid)
            {
                _context.Document.Add(document);

                var       fullPath = $"C:/Users/user/source/repos/Licenta/wwwroot/xml/{document.DocumentPath}";
                XDocument doc      = XDocument.Load(fullPath);

                var balante = from bal in doc.Root.Elements()
                              where bal.Element("cont").Value.ToString().Equals("121")
                              select bal;

                foreach (XElement bal in balante)
                {
                    ProfitPierdere profitPierdere = CreateProfitPierdere(bal, balanta);
                    _context.Add(profitPierdere);
                }

                _fileManager.DeleteDocumentXML(document.DocumentPath);
                _context.Document.Remove(document);
                _context.SaveChanges();
            }
            ViewData["ClientId"] = new SelectList(_context.Client.OrderBy(u => u.Denumire), "ClientId", "Denumire", balanta.ClientId);
            TempData["Message"]  = "Solduri profit-pierdere importate cu succes!";
            TempData["Success"]  = "true";
            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> DeleteAPI(int id)
        {
            ProfitPierdere profitPierdere = _context.ProfitPierdere.Find(id);

            if (profitPierdere == null)
            {
                return(Json(new { success = false, message = "Eroare la stergerea soldurilor de profit/pierdere!" }));
            }
            else
            {
                try
                {
                    _context.ProfitPierdere.Remove(profitPierdere);
                    await _context.SaveChangesAsync();

                    return(Json(new { success = true, message = "Solduri profit/pierdere sterse cu succes!" }));
                }
                catch
                {
                    return(Json(new { success = false, message = "Eroare la stergerea soldurilor de profit/pierdere!" }));
                }
            }
        }