Ejemplo n.º 1
0
        /// <summary>
        /// Método de verificação dos dados do pagamento
        /// </summary>
        /// <param name="payment"></param>
        /// <param name="jobId"></param>
        /// <param name="employerId"></param>
        /// <returns>Invoice ou Exception</returns>
        public Invoice makePayment(Invoice payment, int jobId, int employerId)
        {
            WorkDAO          workDAO = new WorkDAO(_connection);
            WorkDetailsModel job     = workDAO.FindById(jobId);

            payment.ConfirmedPayment = false;

            if (job == null)
            {
                throw new Exception("Trabalho não existe!");
            }

            if (job.Employer.Id != employerId)
            {
                throw new Exception("Não tem acesso a este trabalho!");
            }

            if (!job.FinishedConfirmedByEmployer || !job.FinishedConfirmedByMate)
            {
                throw new Exception("Trabalho não foi confirmado como terminado!");
            }

            bool paymentTypeAllowed = false;

            for (int i = 0; i < job.JobPost.PaymentMethod.Length; i++)
            {
                if (job.JobPost.PaymentMethod[i].Equals(payment.PaymentType))
                {
                    paymentTypeAllowed = true;
                    break;
                }
            }

            if (!paymentTypeAllowed)
            {
                throw new Exception("Método de pagamento inválido para este trabalho!");
            }

            if (payment.PaymentType.Equals(Payment.PAYPAL))
            {
                payment = MakePaymentWithPayPal(payment, job, jobId);
            }

            if (payment.PaymentType.Equals(Payment.CRYPTO))
            {
                Console.WriteLine("Pagamento feito com Crypto!");
            }

            if (payment.PaymentType.Equals(Payment.MBWAY))
            {
                Console.WriteLine("Pagamento feito com MBWay!");
            }

            if (payment.PaymentType.Equals(Payment.MONEY))
            {
                payment = MakePaymentWithMoney(payment, job, jobId);
            }

            return(payment);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Método para confirmar o trabalho como realizado,
        /// removendo-o posteriormente da lista de trabalhos
        /// pendentes do Mate
        /// </summary>
        /// <param name="jobId">Id do trabalho a marcar como concluído</param>
        /// <param name="userId">Id do utilizador que marca o trabalho como
        /// concluído</param>
        /// <returns>Verdadeiro em caso de sucesso, falso caso contrário</returns>
        public bool MarkJobAsDone(int jobId, int userId)
        {
            WorkDAO          workDAO = new WorkDAO(_connection);
            WorkDetailsModel job     = workDAO.FindById(jobId);

            if (job == null)
            {
                throw new Exception("ID de trabalho Inválido!");
            }

            EmployerDAO employerDAO = new EmployerDAO(_connection);
            Employer    employer    = employerDAO.FindEmployerById(userId);

            bool foundEmployer = (employer != null);

            if (foundEmployer)
            {
                using (SqlCommand cmd = _connection.Fetch().CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "UPDATE dbo.[Job] SET FinishedConfirmedByEmployer = @fnshdEmployer " +
                                      "WHERE Id = @id";

                    cmd.Parameters.Add("@id", SqlDbType.Int).Value            = jobId;
                    cmd.Parameters.Add("@fnshdEmployer", SqlDbType.Bit).Value = true;

                    cmd.ExecuteNonQuery();
                }

                return(true);
            }

            MateDAO mateDAO = new MateDAO(_connection);
            Mate    mate    = mateDAO.FindMateById(userId);

            bool foundMate = (mate != null);

            if (foundMate)
            {
                using (SqlCommand cmd = _connection.Fetch().CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "UPDATE dbo.[Job] SET FinishedConfirmedByMate = @fnshdMate " +
                                      "WHERE Id = @id";

                    cmd.Parameters.Add("@id", SqlDbType.Int).Value        = jobId;
                    cmd.Parameters.Add("@fnshdMate", SqlDbType.Bit).Value = true;

                    cmd.ExecuteNonQuery();
                }

                return(true);
            }

            throw new Exception("ID do utilizador Inválido!");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Método para obter um Invoice por Id
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="jobId"></param>
        /// <returns>Invoice ou Exception</returns>
        public Invoice GetInvoiceByID(int userId, int jobId)
        {
            WorkDAO          workDAO = new WorkDAO(_connection);
            WorkDetailsModel job     = workDAO.FindById(jobId);

            if (job == null)
            {
                throw new Exception("Trabalho não existe!");
            }

            if (job.Employer.Id != userId && job.Mate.Id != userId)
            {
                throw new Exception("Não tem autorização para aceder a este pagamento!");
            }

            Invoice invoice = null;

            using (SqlCommand cmd = _connection.Fetch().CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Select Id, Value, PaymentTypeId, Date, ConfirmedPayment FROM dbo.[Invoice] where Id=@id";

                cmd.Parameters.Add("@id", SqlDbType.Int).Value = job.InvoiceId;

                using SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    invoice = new Invoice();
                    reader.Read();

                    invoice.Id               = reader.GetInt32(0);
                    invoice.Value            = reader.GetDouble(1);
                    invoice.PaymentType      = (Payment)reader.GetInt32(2);
                    invoice.Date             = reader.GetDateTime(3);
                    invoice.ConfirmedPayment = reader.GetBoolean(4);
                }
            }

            if (invoice == null)
            {
                throw new Exception("Pagamento ainda não foi realizado!");
            }

            return(invoice);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Método para confirmar o pagamento
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="mateId"></param>
        /// <returns>bool ou Exception</returns>
        public bool confirmPayment(int jobId, int mateId)
        {
            WorkDAO          workDAO = new WorkDAO(_connection);
            WorkDetailsModel job     = workDAO.FindById(jobId);

            if (job == null)
            {
                throw new Exception("Trabalho não existe!");
            }

            if (job.Mate.Id != mateId)
            {
                throw new Exception("Não tem acesso a este trabalho!");
            }

            Invoice invoice = this.GetInvoiceByID(mateId, jobId);

            if (invoice == null)
            {
                throw new Exception("Pagamento ainda não foi realizado!");
            }

            using (SqlCommand cmdSetInvoice = _connection.Fetch().CreateCommand())
            {
                cmdSetInvoice.CommandText = "UPDATE dbo.[Invoice]" +
                                            "SET ConfirmedPayment = @cfrmdPmnt " +
                                            "WHERE dbo.[Invoice].Id = @Id";

                cmdSetInvoice.Parameters.Add("@Id", SqlDbType.Int).Value        = invoice.Id;
                cmdSetInvoice.Parameters.Add("@cfrmdPmnt", SqlDbType.Bit).Value = true;

                cmdSetInvoice.ExecuteScalar();
            }

            return(true);
        }