Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Id,Livre_Emporte,Client,Num_Pizza,Qte_Pizza,Ligne_Commandes")] SaveCommandModel commandView, string command)
        {
            if (command == "Ajouter une commande")
            {
                var lines = TempData.Get <IList <SaveCommandLinesModel> >("ligne");
                var pizza = await _pizzaService.GetPizzaById(commandView.Num_Pizza);

                var pizzaViewModel = _mapper.Map <PizzaViewModel>(pizza);
                if (lines != null && lines.Any())
                {
                    lines.Add(new SaveCommandLinesModel {
                        Num_Pizza = commandView.Num_Pizza, Quantité = commandView.Qte_Pizza.ToString(), Pizza = pizzaViewModel
                    });
                    _lignesCommande = lines.ToList();
                }
                else
                {
                    lines = new List <SaveCommandLinesModel>  {
                        new SaveCommandLinesModel {
                            Num_Pizza = commandView.Num_Pizza, Quantité = commandView.Qte_Pizza.ToString(), Pizza = pizzaViewModel
                        }
                    };
                    _lignesCommande = lines.ToList();
                }
                TempData.Put("ligne", lines);

                return(await Create());
            }
            else
            {
                var lines = TempData.Get <IList <SaveCommandLinesModel> >("ligne");
                commandView.Ligne_Commandes = lines;

                var validator        = new SaveCommandModelValidator();
                var validationResult = await validator.ValidateAsync(commandView);

                if (!validationResult.IsValid)
                {
                    return(BadRequest(validationResult.Errors));
                }

                var clientId = await _clientService.GetClientIdByName(commandView.Client.Nom_Cli);

                if (clientId == null)
                {
                    var adresseId = await GetAdressIdForCustomer(commandView.Client.Adresse, commandView.Client.Num_Quartier);

                    var clientToCreate = new Client {
                        Nom_Cli = commandView.Client.Nom_Cli, Num_Adresse = adresseId
                    };
                    await _clientService.AddClient(clientToCreate);

                    clientId = clientToCreate.Id;
                }

                commandView.Num_Cli  = (int)clientId;
                commandView.Date_Cde = DateTime.Now;
                var commandToCreate = _mapper.Map <SaveCommandModel, CdeCli>(commandView);

                await _commandService.CreateCommand(commandToCreate);

                //Recup l'id de la nouvelle commande créée pour les lignes de commande
                var cdeId = await _commandService.GetLastCommandId();

                if (cdeId != null)
                {
                    var montantTotal = 0;

                    foreach (var line in lines)
                    {
                        line.Num_Cde_Cli = cdeId;

                        // Création de la fabrication
                        var fab = new Fabrication {
                            Num_Pizza = line.Num_Pizza, Quant_Fab = int.Parse(line.Quantité), Date_Fab = DateTime.Now
                        };
                        await _commandService.AddFabrication(fab);

                        montantTotal += (int)line.MontantTotal;
                    }

                    // Création des lignes de commande client
                    var linesToCreate = _mapper.Map <IEnumerable <SaveCommandLinesModel>, IEnumerable <Ligne_Cde_Cli> >(lines);
                    await _commandService.AddCommandLines(linesToCreate);

                    // Création de la facture
                    var factureToCreate = new Fact_Cli_BonLiv {
                        Num_Cli = commandView.Num_Cli, Montant_Total = montantTotal, Date_Fact_Cli = DateTime.Now.Date
                    };
                    await _commandService.CreateFacture(factureToCreate);

                    // Création du bon de livraison
                    var factureId = await _commandService.GetLastFactureId();

                    var bonLivToCreate = new BonLiv {
                        Num_Cde_Cli = (int)cdeId, Num_Fact = factureId, Date_Liv = DateTime.Now
                    };
                    await _commandService.CreateBonLiv(bonLivToCreate);
                }

                return(RedirectToAction("Index", "ResumeCommand"));
            }
        }
Beispiel #2
0
 public async Task CreateFacture(Fact_Cli_BonLiv facture)
 {
     await _unitOfWork.Facturations.CreateFacturation(facture);
 }