@ows_payment_closing_balances
@lambda_documents_payout_all
Feature: Abacus Payments 2

  Background:
    Given an orchard employee label_7123_admin_3 for abacus
    When I log in

  @frontend_royalties_payment
  @ows_royalties_closing_balances
  @ows_abacus_event_closing_balances
  @ows_abacus_state_closing_balances
  @lambda_documents_closing_balances
  @lambda_documents_payout_closing_balances
  @cleanup_payment_groups
  @id_6c1dd6626e098c9a13c7aece7d383e4a67265894627fda2a62d2e1ee0cd7beab
  Scenario: Payment group payment accounts and info pop up
    Given I prepare an abacus account data:
      | Key              | Value                   |
      | companyBrandName | awal                    |
      | paidBy           | AWAL-UK                 |
      | agreementType    | AWAL Core               |
      | currency         | GBP                     |
      | owner            | awal                    |
      | paymentMinimum   | 100                     |
      | paymentSchedule  | 45 days after month end |
    And I create 1 new account via API
    And I have a Distribution runcontroller created
    And I have a new Distribution contract lifecycle created with accounts created for AWAL Recordings Licensing Ltd
    And I complete the account to create payment in payment entity 100 and update tax_eligibility to complete
    And I have a new payment group created
    When I should be able to add account to a payment group payment
    When payment details should be shown at payment info popup
    When I should be able to see the account on a group payment page
    And I should be able to see an entry on payment overview section
    And I should be able to see an entry on payments page

  @ows_royalties_closing_balances
  @ows_abacus_event_closing_balances
  @ows_abacus_state_closing_balances
  @lambda_documents_closing_balances
  @lambda_documents_payout_closing_balances
  @cleanup_payment_groups
  @id_66b4c1750a83afcba754ca2d26ed721d7c87a46cbc24d446e45d3473671d2309
  Scenario: Approve/send payment for PAYCHEX payment method
    Given I prepare an abacus account data:
      | Key              | Value                     |
      | companyBrandName | awal                      |
      | paidBy           | AWAL-UK                   |
      | agreementType    | AWAL Core                 |
      | currency         | GBP                       |
      | owner            | awal                      |
      | paymentMinimum   | 100                       |
      | paymentSchedule  | 60 days after quarter end |
    And I create 1 new active account via API
    And I have a Distribution runcontroller created
    And I have a new Distribution contract lifecycle created with accounts created for AWAL Recordings Licensing Ltd
    And I update account_payee and account_tax_info tables
    And I complete the account to create payment in payment entity 100 and update tax_eligibility to complete
    And I update payoneer id in DB to 100246640
    And I have a new payment group created
    And I update payment group criteria to PAYCHEX
    And I add closing balance to the account for current statement period
    And I add taxable revenue to the account for current statement period
    And I update payment method in DB to PAYCHEX
    When I should be able to create a payment for payment group created
    When I should be able to see generated Payment
    And I should be able to see account name on a group payment page
    And I should be able to review and approve the Balance payment
    And I send payment to Payoneer
    And I click done button
    And I update payment method in DB to PAYONEER

  @ows_royalties_closing_balances
  @ows_abacus_event_closing_balances
  @ows_abacus_state_closing_balances
  @lambda_documents_closing_balances
  @frontend_royalties_payment
  @lambda_documents_payout_closing_balances
  @id_73a956bb5e3ebbd3ee58ef95111b8994f64eeacc30c75fc4e170e33067258c3e
  @deleteWorksheetAccountContractClosingBalance
  Scenario: Approve/send payment and pending payment
    Given I prepare an abacus account data:
      | Key              | Value                   |
      | companyBrandName | awal                    |
      | paidBy           | AWAL-UK                 |
      | agreementType    | AWAL Core               |
      | currency         | GBP                     |
      | owner            | awal                    |
      | paymentMinimum   | 100                     |
      | paymentSchedule  | 45 days after month end |
    And I create 2 new active accounts via API
    And I have a Distribution runcontroller created
    And I have a new Distribution contract lifecycle created with accounts created for AWAL Recordings Licensing Ltd
    And I update account_payee and account_tax_info tables
    And I complete the account to create payment in payment entity 100 and update tax_eligibility to complete
    And I update payoneer id in DB to 100246640
    And I add closing balance to the account for previous statement period
    And I add taxable revenue to the account for previous statement period
    And I should be able to add a payment for specific accounts
    And I should be able to see generated Payment
    And I should be able to see account name on a group payment page
    And I should be able to add a note for the payment
    And I should be able to review and approve the Balance payment
    And I should be able to add a payment for specific accounts
    Then payment group should contain a pending payment

  @ows_royalties_closing_balances
  @ows_abacus_event_closing_balances
  @ows_abacus_state_closing_balances
  @lambda_documents_closing_balances
  @frontend_royalties_payment
  @lambda_documents_payout_closing_balances
  @id_f52d379544a1cc9169e3d92dbd9e265f9a70ac37f31703489624f747ea1bbb3b
  Scenario: Payment does not go to ledger after payment has failed to verify from Payoneer
    Given I prepare an abacus account data:
      | Key              | Value                   |
      | companyBrandName | awal                    |
      | paidBy           | AWAL-UK                 |
      | agreementType    | AWAL Core               |
      | currency         | GBP                     |
      | owner            | awal                    |
      | paymentMinimum   | 100                     |
      | paymentSchedule  | 45 days after month end |
    And I create 1 new active account via API
    And I have a Distribution runcontroller created
    And I have a new Distribution contract lifecycle created with accounts created for AWAL Recordings Licensing Ltd
    And I update account_payee and account_tax_info tables
    And I add closing balance to the account for current statement period
    And I add taxable revenue to the account for current statement period
    And I complete the account to create payment in payment entity 100 and update tax_eligibility to complete
    And I update payoneer id in DB to 0
    And I should be able to add a payment for specific accounts
    And I should be able to see generated Payment

  @lambda_documents_closing_balances
  @lambda_documents_payout_closing_balances
  @deleteWorksheetAccountContractClosingBalance
  @id_ba5599243b0cc6a3c53bc0ca74e42cfb19d255269b7ea798b05b3f96f9a887c0
  Scenario: Flowthrough payment above closed balance - create payment with flowthrough records that are higher than the closed balance
    Given I prepare an abacus account data:
      | Key              | Value                   |
      | companyBrandName | awal                    |
      | paidBy           | AWAL-UK                 |
      | agreementType    | AWAL Core               |
      | currency         | EUR                     |
      | owner            | awal                    |
      | paymentMinimum   | 100                     |
      | paymentSchedule  | 45 days after month end |
    And I create 1 new active account via API
    # we need to have payoneer id with disabled rules to trigger webhooks for testing purposes, so we trigger webhooks with tests
    And I update payoneer id in DB to 100246490
    And I have a Distribution runcontroller created
    And I have a new Distribution contract lifecycle created with accounts created for AWAL Recordings Licensing Ltd
    And I update account_payee and account_tax_info tables
    And I complete the account to create payment in payment entity 100 and update tax_eligibility to complete
    And I add closing balance to the account for current statement period with amount -10.00
    And I add taxable revenue to the account for current statement period
    And I add flowthrough payment allocation records to the account
      | amount | currency |
      | 600.00 | USD      |
      | 500.00 | USD      |
    Then flowthrough payment allocation statuses should have payment status init and ledger status init
    And I should be able to add a payment for specific accounts
    And I should be able to see generated Payment
    And I should be able to see account name on a group payment page
    Then flowthrough payment allocation statuses should have payment status attached_to_payment and ledger status attached_to_payment
    And I should be able to review and approve the Balance payment
    And I delete the payment
    Then flowthrough payment allocation statuses should have payment status init and ledger status init
    And I should be able to add a payment for specific accounts
    And I should be able to see generated Payment
    And I should be able to see account name on a group payment page
    Then flowthrough payment allocation statuses should have payment status attached_to_payment and ledger status attached_to_payment
    And I should be able to review and approve the Balance payment
    And I send payment to Payoneer
    And I click done button
    Then I trigger webhook to make Balance payment approved from payoneer
    Then I trigger webhook to make Balance payment completed from payoneer
    Then flowthrough payment allocation statuses should have payment status paid and ledger status debited
    Then I trigger webhook to make Balance payment canceled from payoneer
    Then flowthrough payment allocation statuses should have payment status returned and ledger status returned
    When I should be able to open account details
    Then I check that Account Activity has balance payment event Payment Returned with amount 1100.00
    Then I can check that amount in worksheet_account_contract_payable_details is 1100.00 for flowthrough
    Then I can check that amount in worksheet_account_contract_payable_details is 0.00 for closing balance

  @lambda_documents_closing_balances
  @lambda_documents_payout_closing_balances
  @deleteWorksheetAccountContractClosingBalance
  @id_5cf626de6f44b024ae91407077a5365eb26641a51c42d2693541e693b5decb99
  Scenario: Flowthrough payment equal to closed balance - create payment with flowthrough records that are equal to the closed balance
    Given I prepare an abacus account data:
      | Key              | Value                   |
      | companyBrandName | awal                    |
      | paidBy           | AWAL-UK                 |
      | agreementType    | AWAL Core               |
      | currency         | EUR                     |
      | owner            | awal                    |
      | paymentMinimum   | 100                     |
      | paymentSchedule  | 45 days after month end |
    And I create 1 new active account via API
    # we need to have payoneer id with disabled rules to trigger webhooks for testing purposes, so we trigger webhooks with tests
    And I update payoneer id in DB to 100246490
    And I have a Distribution runcontroller created
    And I have a new Distribution contract lifecycle created with accounts created for AWAL Recordings Licensing Ltd
    And I update account_payee and account_tax_info tables
    And I complete the account to create payment in payment entity 100 and update tax_eligibility to complete
    And I add closing balance to the account for current statement period with amount 1000.00
    And I add taxable revenue to the account for current statement period
    And I add flowthrough payment allocation records to the account
      | amount | currency |
      | 500.00 | USD      |
      | 500.00 | USD      |
    And I should be able to add a payment for specific accounts
    And I should be able to see generated Payment
    And I should be able to see account name on a group payment page
    Then flowthrough payment allocation statuses should have payment status attached_to_payment and ledger status attached_to_payment
    And I should be able to review and approve the Balance payment
    And I send payment to Payoneer
    And I click done button
    Then I trigger webhook to make Balance payment approved from payoneer
    Then I trigger webhook to make Balance payment completed from payoneer
    Then flowthrough payment allocation statuses should have payment status paid and ledger status debited
    Then I trigger webhook to make Balance payment canceled from payoneer
    Then flowthrough payment allocation statuses should have payment status returned and ledger status returned
    When I should be able to open account details
    Then I check that Account Activity has balance payment event Payment Returned with amount 1000.00
    Then I can check that amount in worksheet_account_contract_payable_details is 0 for flowthrough
    Then I can check that amount in worksheet_account_contract_payable_details is 1000.00 for closing balance
