Exemplo n.º 1
0
        public MasterContract GetMasterContract(int masterContractId)
        {
            ContractDAO    contractDAO    = new ContractDAO(connectionString);
            MasterContract masterContract = contractDAO.GetMasterContract(masterContractId);

            return(masterContract);
        }
Exemplo n.º 2
0
        public List <NormalContract> CheckNormalContractsFromMasterContract(int masterContractId)
        {
            List <NormalContract> contractsNotFound = new List <NormalContract>();
            ContractDAO           contractDAO       = new ContractDAO(connectionString);
            MasterContract        masterContract    = contractDAO.GetMasterContract(masterContractId);

            this.MakeContractsFromMasterItems(masterContract);

            List <NormalContract> normalContractsThatShouldExist = this.MakeContractsFromMasterItems(masterContract);

            foreach (NormalContract normalContractThatShouldExist in normalContractsThatShouldExist)
            {
                IEnumerable <NormalContract> contractsFound = from existingNormalContract in masterContract.NormalContracts
                                                              where existingNormalContract.StartDate == normalContractThatShouldExist.StartDate && existingNormalContract.EndDate == normalContractThatShouldExist.EndDate
                                                              select existingNormalContract;

                if (contractsFound.Count() == 0)
                {
                    contractsNotFound.Add(normalContractThatShouldExist);
                    continue;
                }
                else
                {
                    foreach (NormalContractItem itemThatShouldExist in normalContractThatShouldExist.NormalContractItems)
                    {
                        IEnumerable <NormalContractItem> itemsFound = from existingNormalContract in contractsFound
                                                                      from existingItem in existingNormalContract.NormalContractItems
                                                                      where existingItem.Material.Id == itemThatShouldExist.Material.Id && existingItem.Quantity == itemThatShouldExist.Quantity
                                                                      select existingItem;
                        if (itemsFound.Count() == 0)
                        {
                            contractsNotFound.Add(normalContractThatShouldExist);
                            continue;
                        }
                    }
                }
            }
            return(contractsNotFound);
        }
Exemplo n.º 3
0
        public Result SendMasterContractToSAP(int masterContractId, int userId)
        {
            Result          finalResult     = new Result(success: false);
            ContractService contractService = new ContractService();
            ContractDAO     contractDAO     = new ContractDAO(connectionString);

            try
            {
                MasterContract        masterContract  = contractDAO.GetMasterContract(masterContractId);
                List <NormalContract> normalContracts = contractService.MakeContractsFromMasterItems(masterContract);

                if (normalContracts.Count > 0)
                {
                    contractDAO.SetQuantityOfContractsInMasterContract(masterContractId, normalContracts.Count);
                    List <SendToSAPResult> allResultsFromSendToSAP = new List <SendToSAPResult>();

                    foreach (NormalContract normalContract in normalContracts)
                    {
                        normalContract.CreationUser = new User {
                            Id = userId
                        };
                        GenerateAndSaveNormalContractAndItems(normalContract);
                        List <Result> listOfResultsForThisContract = SendNormalContractToSAP(normalContract);
                        var           resultsWithFailures          = from contractResult in listOfResultsForThisContract
                                                                     where contractResult.Success == false
                                                                     select contractResult;
                        allResultsFromSendToSAP.Add(new SendToSAPResult
                        {
                            NormalContract = normalContract,
                            Payload        = listOfResultsForThisContract,
                            Success        = (resultsWithFailures.Count() == 0)
                        });
                    }
                    var failedResults = from r in allResultsFromSendToSAP
                                        from r1 in r.Payload as List <Result>
                                        where r1.Success == false
                                        select r1;
                    if (failedResults.Count() == 0)
                    {
                        finalResult.Success = true;
                    }

                    // Update the Master Contract as generated and sent to SAP.
                    masterContract.GeneratorUser = new User {
                        Id = userId
                    };
                    masterContract.SenderToSAPUser = new User {
                        Id = userId
                    };
                    contractDAO.SetMasterContractSentToSAP(masterContract);

                    finalResult.Payload = allResultsFromSendToSAP;
                }
                else
                {
                    finalResult.Messages.Add("Contratos não gerados.");
                }
            }
            catch (Exception ex)
            {
                finalResult.Messages.Add(ex.Message);
            }
            return(finalResult);
        }