ALTER VIEW payments_view (id, user_id, author_name, payment_paypal_email, project_id, project_name, offer_id, offer_name,total_gross_amount, total_project_amount, total_copyscape_cost, payment_id, payment_status, paypal_payout_batch_id, paypal_status, total_amount, project_amount, copyscape_cost
) AS
  SELECT
    a.id,
    a.user_id                   AS user_id,
    CONCAT(a.first_name, ' ', a.last_name),
    a.payment_paypal_email,
    p.id                   AS project_id,
    p.name                 AS project_name,
    po.id                  AS offer_id,
    po.offer_name          AS offer_name,
    SUM(op.gross_amount)   AS total_gross_amount,
    SUM(op.net_amount)     AS total_project_amount,
    SUM(op.copyscape_cost) AS total_copyscape_cost,
    pp.id                  AS payment_id,
    pp.status              AS payment_status,
    pp.paypal_payout_batch_id,
    pp.paypal_status,
    pp.total_amount,
    pp.project_amount,
    pp.copyscape_cost
  FROM offer_partitipants AS op
    JOIN project_offers AS po ON op.offer_id = po.id
    JOIN projects AS p ON po.project_id = p.id
    JOIN authors AS a ON op.user_id = a.user_id
    LEFT JOIN offer_participant_payments AS pp ON pp.user_id = a.user_id AND op.offer_id = po.id
  WHERE op.project_status = 1 AND (pp.status IS NULL OR pp.status <> 1) AND a.id IS NOT NULL
  GROUP BY op.user_id;