Esempio n. 1
0
        public static async Task <WSCreatePreInvoice.Create_Result> CreateContractInvoice(AutorizarFaturaçãoContratos CreateInvoice, NAVWSConfigurations WSConfigurations, string ContractInvoicePeriod, string InvoiceBorrowed, string CodTermosPagamento, bool PricesIncludingVAT, string Ship_to_Code)
        {
            DateTime           now             = DateTime.Now;
            string             PostingNoSeries = "";
            string             Observacoes     = "";
            string             Mes             = InvoiceBorrowed.Substring(0, InvoiceBorrowed.IndexOf("/"));
            string             Ano             = InvoiceBorrowed.Substring(InvoiceBorrowed.IndexOf("/") + 1, 4);
            ConfigUtilizadores CUsers          = DBUserConfigurations.GetById(CreateInvoice.UtilizadorCriação);
            Contratos          Contrato        = DBContracts.GetByIdLastVersion(CreateInvoice.NºContrato);

            WSCreatePreInvoice.Create_Result result = new WSCreatePreInvoice.Create_Result();


            if (Contrato != null && Contrato.TipoContrato == 3 && Contrato.Tipo == 3) //Contrato Quotas
            {
                ConfiguracaoParametros Parametro = DBConfiguracaoParametros.GetByParametro("QuotasNoSeries");

                if (Parametro != null && !string.IsNullOrEmpty(Parametro.Valor))
                {
                    PostingNoSeries = Parametro.Valor;
                }

                if (Contrato != null && !string.IsNullOrEmpty(Contrato.TextoFatura))
                {
                    Observacoes = Contrato.TextoFatura;
                    Observacoes = Observacoes.Replace("<MES>", Mes);
                    Observacoes = Observacoes.Replace("<ANO>", Ano);
                }
            }
            else
            {
                PostingNoSeries = !string.IsNullOrEmpty(CUsers.NumSerieFaturas) ? CUsers.NumSerieFaturas : "";
                Observacoes     = !string.IsNullOrEmpty(CreateInvoice.Descrição) ? CreateInvoice.Descrição : "";
            }

            WSCreatePreInvoice.Create NAVCreate = new WSCreatePreInvoice.Create()
            {
                WSPreInvoice = new WSCreatePreInvoice.WSPreInvoice()
                {
                    Sell_to_Customer_No          = !string.IsNullOrEmpty(CreateInvoice.NºCliente) ? CreateInvoice.NºCliente : "",
                    Document_Date                = DateTime.Today,
                    Document_DateSpecified       = true,
                    Shipment_Date                = now,
                    Shipment_DateSpecified       = true,
                    Shipment_Start_Time          = now.AddHours(1),
                    Shipment_Start_TimeSpecified = true,
                    Document_Type                = WSCreatePreInvoice.Document_Type.Invoice,
                    Document_TypeSpecified       = true,
                    Posting_Date             = CreateInvoice.DataDeRegisto ?? DateTime.Now,
                    Posting_DateSpecified    = true,
                    Periodo_de_Fact_Contrato = !string.IsNullOrEmpty(ContractInvoicePeriod) ? ContractInvoicePeriod : "",
                    Data_Serv_Prestado       = !string.IsNullOrEmpty(InvoiceBorrowed) ? InvoiceBorrowed : "",
                    Responsibility_Center    = !string.IsNullOrEmpty(CUsers.CentroDeResponsabilidade) ? CUsers.CentroDeResponsabilidade : "",

                    Posting_No_Series  = PostingNoSeries,
                    Due_Date           = (DateTime)CreateInvoice.DataDeExpiração,
                    Due_DateSpecified  = true,
                    Payment_Terms_Code = CodTermosPagamento,

                    //Amaro
                    Observacoes             = Observacoes,
                    Contract_No             = !string.IsNullOrEmpty(CreateInvoice.NºContrato) ? CreateInvoice.NºContrato : "",
                    Factura_CAF             = true,
                    Factura_CAFSpecified    = true,
                    Codigo_Pedido           = !string.IsNullOrEmpty(CreateInvoice.NoRequisicaoDoCliente) ? CreateInvoice.NoRequisicaoDoCliente : "",
                    No_Compromisso          = !string.IsNullOrEmpty(CreateInvoice.NoCompromisso) ? CreateInvoice.NoCompromisso : "",
                    Data_Encomenda          = CreateInvoice.DataRececaoRequisicao ?? DateTime.MinValue,
                    Data_EncomendaSpecified = true,

                    RegionCode20               = !string.IsNullOrEmpty(CreateInvoice.CódigoRegião) ? CreateInvoice.CódigoRegião : "",
                    FunctionAreaCode20         = !string.IsNullOrEmpty(CreateInvoice.CódigoÁreaFuncional) ? CreateInvoice.CódigoÁreaFuncional : "",
                    ResponsabilityCenterCode20 = !string.IsNullOrEmpty(CreateInvoice.CódigoCentroResponsabilidade) ? CreateInvoice.CódigoCentroResponsabilidade : "",

                    Prices_Including_VAT          = PricesIncludingVAT,
                    Prices_Including_VATSpecified = true,

                    Ship_to_Code = !string.IsNullOrEmpty(Ship_to_Code) ? Ship_to_Code : "",
                }
            };

            // Configure NAV Client
            EndpointAddress WS_URL = new EndpointAddress(WSConfigurations.WS_PreInvoice_URL.Replace("Company", WSConfigurations.WS_User_Company));

            WSCreatePreInvoice.WSPreInvoice_PortClient WS_Client = new WSCreatePreInvoice.WSPreInvoice_PortClient(navWSBinding, WS_URL);
            WS_Client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
            WS_Client.ClientCredentials.Windows.ClientCredential          = new NetworkCredential(WSConfigurations.WS_User_Login, WSConfigurations.WS_User_Password, WSConfigurations.WS_User_Domain);

            try
            {
                result = await WS_Client.CreateAsync(NAVCreate);

                return(result);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Esempio n. 2
0
        public JsonResult Create([FromBody] FornecedorDetailsViewModel data)
        {
            if (data != null)
            {
                if (string.IsNullOrEmpty(data.No))
                {
                    data.No = "";
                }
                if (string.IsNullOrEmpty(data.Name))
                {
                    data.Name = "";
                }
                if (string.IsNullOrEmpty(data.PostCode))
                {
                    data.PostCode = "";
                }
                if (string.IsNullOrEmpty(data.City))
                {
                    data.City = "";
                }
                if (string.IsNullOrEmpty(data.Country))
                {
                    data.Country = "";
                }
                if (string.IsNullOrEmpty(data.Phone))
                {
                    data.Phone = "";
                }
                if (string.IsNullOrEmpty(data.Email))
                {
                    data.Email = "";
                }
                if (string.IsNullOrEmpty(data.Fax))
                {
                    data.Fax = "";
                }
                if (string.IsNullOrEmpty(data.HomePage))
                {
                    data.HomePage = "";
                }
                if (string.IsNullOrEmpty(data.VATRegistrationNo))
                {
                    data.VATRegistrationNo = "";
                }
                if (string.IsNullOrEmpty(data.PaymentTermsCode))
                {
                    data.PaymentTermsCode = "";
                }
                if (string.IsNullOrEmpty(data.PaymentMethodCode))
                {
                    data.PaymentMethodCode = "";
                }
                if (string.IsNullOrEmpty(data.NoClienteAssociado))
                {
                    data.NoClienteAssociado = "";
                }
                if (data.Blocked == null)
                {
                    data.Blocked = 0;
                }
                if (string.IsNullOrEmpty(data.Address))
                {
                    data.Address = "";
                }
                if (string.IsNullOrEmpty(data.Address_2))
                {
                    data.Address_2 = "";
                }
                if (string.IsNullOrEmpty(data.Distrito))
                {
                    data.Distrito = "";
                }
                if (data.Criticidade == null)
                {
                    data.Criticidade = 0;
                }
                if (string.IsNullOrEmpty(data.Observacoes))
                {
                    data.Observacoes = "";
                }

                data.Utilizador_Alteracao_eSUCH = User.Identity.Name;
                var createVendorTask = WSVendorService.CreateAsync(data, _configws);
                try
                {
                    createVendorTask.Wait();
                }
                catch (Exception ex)
                {
                    data.eReasonCode = 3;
                    data.eMessage    = "Ocorreu um erro ao criar o fornecedor no NAV.";
                    data.eMessages.Add(new TraceInformation(TraceType.Error, ex.Message));
                    return(Json(data));
                }

                var result = createVendorTask.Result;
                if (result == null)
                {
                    data.eReasonCode = 3;
                    data.eMessage    = "Ocorreu um erro ao criar o fornecedor no NAV.";
                    return(Json(data));
                }

                data.eReasonCode = 1;

                var vendor = WSVendorService.MapVendorNAVToVendorModel(result.WSVendor);

                if (vendor != null)
                {
                    //SUCESSO
                    vendor.eReasonCode = 1;

                    //Envio de email
                    ConfiguracaoParametros Parametro = DBConfiguracaoParametros.GetByParametro("AddFornecedorEmail");
                    ConfigUtilizadores     UserEmail = DBUserConfigurations.GetById(User.Identity.Name);

                    if (Parametro != null && !string.IsNullOrEmpty(Parametro.Valor))
                    {
                        SendEmailApprovals Email = new SendEmailApprovals();

                        var    path_tmp       = Path.Combine(_generalConfig.FileUploadFolder + "Fornecedores\\tmp\\", data.NomeAnexo);
                        string FileName_Final = data.NomeAnexo.Replace("FORNECEDOR", vendor.No);
                        var    path_final     = Path.Combine(_generalConfig.FileUploadFolder + "Fornecedores\\", FileName_Final);

                        FileStream file_tmp   = new FileStream(path_tmp, FileMode.Open);
                        FileStream file_final = new FileStream(path_final, FileMode.CreateNew);

                        file_tmp.CopyTo(file_final);

                        file_tmp.Dispose();
                        file_final.Dispose();

                        System.IO.File.Delete(path_tmp);

                        Anexos newfile = new Anexos();
                        newfile.NºOrigem          = vendor.No;
                        newfile.UrlAnexo          = FileName_Final;
                        newfile.TipoOrigem        = TipoOrigemAnexos.Fornecedores;
                        newfile.DataHoraCriação   = DateTime.Now;
                        newfile.UtilizadorCriação = User.Identity.Name;

                        Email.DisplayName = "e-SUCH - Fornecedor";
                        Email.From        = "*****@*****.**";
                        Email.To.Add(Parametro.Valor);
                        Email.BCC.Add("*****@*****.**");
                        Email.BCC.Add("*****@*****.**");
                        Email.Subject    = "e-SUCH - Novo Fornecedor";
                        Email.Body       = MakeEmailBodyContent("Criado o Fornecedor:  " + vendor.No + " - " + vendor.Name, UserEmail.Nome);
                        Email.Anexo      = path_final;
                        Email.IsBodyHtml = true;

                        Email.SendEmail_Simple();
                    }

                    return(Json(vendor));
                }
            }
            return(Json(data));
        }