2

I want to import a list of contacts from a csv file (specifically: the Contact IDs), and create a Contribution via API for each one of them. Purpose: These contacts are all ordering the same articles, but have not yet ordered via the CiviContribute system, so we need to import the data.

I wrote a PHP script for that, which is saving the new Contribution correctly on each Contact. It contains the following API call:

civicrm_api3('Contribution', 'create', [
        'financial_type_id' => 11,
        'receive_date' => 20201008150000,
        'total_amount' => 11.15,
        'contact_id' => $civicrm_id,
        'contribution_status_id' => "Pending", 
        'payment_instrument_id' => "Überweisung", 
        'is_pay_later' => 1,
        'price_set_id' => 25,
        'skipLineItem' => 1,
        'api.line_item.create' => [
          '0' => [
            'price_field_id' => 55,
            'qty' => 1,
            'label' => 'article name example 1',
            'line_total' => 8.57,
            'unit_price' => 8.57,
          ],
          '1' => [
            'price_field_id' => 56,
            'qty' => 1,
            'label' => 'article name example 1',
            'line_total' => 2.05,
            'unit_price' => 2.05,
          ]
        ]
      ]);

But it makes two mistakes:

  • The value in the "Item" column in the Contribution View and the Invoice is a default value "line item". I manage to solve this by adding the article name into the code ('label' => 'article name example 1'), but it should be automatic.

  • The tax is not added in the Contribution View and the Invoice. I think this shows that the new Contribution is not properly connected with its Financial Type (or Price Set?), because the tax is calculated automatically depending on the Financial Type, I think. I added the 'financial_type_id' to the api Call, but the problem persists.

Any hint anybody?

0

This should work

civicrm_api3('Contribution', 'create', [
    'financial_type_id' => 11,
    'receive_date' => 0201008150000,
    'total_amount' => 11.15,
    'tax_amount' => 0.53,
    'net_amount' => 10.62,
    'contact_id' => $civicrm_id,
    'contribution_status_id' => "Pending", 
    'payment_instrument_id' => "Überweisung", 
    'is_pay_later' => 1,
    'skipLineItem' => 1,
    'api.line_item.create' => [
        'entity_table' => 'civicrm_contribution',
        'contribution_type_id' => 11,
        'financial_type_id' => 11,
        'price_field_id' => 55,
        'label' => 'article name example 1',
        'tax_amount' => 0.53,
        'unit_price' => 10.62,
        'line_total' => 10.62,
        'non_deductible_amount' => 0.00,
        'qty' => 1.00,
      ]
  ]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.