mecachisenros / woocommerce_civicrm

GNU Affero General Public License v3.0
14 stars 16 forks source link

Convert to Order API for line item support #33

Closed agileware-fj closed 3 years ago

agileware-fj commented 3 years ago

This converts the integration to use the Order API to allow adding line items that are linked properly to the resulting contribution.

This might be related to or at least interfere with work on #32

monishdeb commented 3 years ago

I have applied the patch on one of our staging site to test it correctly records separate line-item and and financial item on purchasing an order with multiple products. I have added an order with two product items:

  1. Test Product (National) - Green ($50)
  2. PEI T-Shirt ($18)

Here's the financial records before and after applying the patch:

BEFORE

civicrm_api3('LineItem', 'get', [
  'sequential' => 1,
  'contribution_id' => 25955,
  'api.FinancialItem.get' => ['entity_id' => "\$value.id", 'entity_table' => "civicrm_line_item"],
]);
// result
{

    "is_error": 0,
    "version": 3,
    "count": 2,
    "values": [
        {
            "id": "122651",
            "entity_table": "civicrm_contribution",
            "entity_id": "25955",
            "contribution_id": "25955",
            "price_field_id": "1",
            "label": "PEI T-Shirt",
            "qty": "1.00",
            "unit_price": "68.00",
            "line_total": "68.00",
            "financial_type_id": "60",
            "non_deductible_amount": "0.00",
            "contribution_type_id": "60",
            "api.FinancialItem.get": {
                "is_error": 0,
                "version": 3,
                "count": 0,
                "values": []
            }
        },
        {
            "id": "122652",
            "entity_table": "civicrm_contribution",
            "entity_id": "25955",
            "contribution_id": "25955",
            "price_field_id": "1",
            "label": "Test Product (National) - Green",
            "qty": "1.00",
            "unit_price": "68.00",
            "line_total": "68.00",
            "financial_type_id": "60",
            "non_deductible_amount": "0.00",
            "contribution_type_id": "60",
            "api.FinancialItem.get": {
                "is_error": 0,
                "version": 3,
                "count": 0,
                "values": []
            }
        }
    ]
}

AFTER

{

    "is_error": 0,
    "version": 3,
    "count": 2,
    "values": [
        {
            "id": "122665",
            "entity_table": "civicrm_contribution",
            "entity_id": "25960",
            "contribution_id": "25960",
            "price_field_id": "1",
            "label": "Test Product (National) - Green",
            "qty": "1.00",
            "unit_price": "68.00",
            "line_total": "68.00",
            "financial_type_id": "60",
            "non_deductible_amount": "0.00",
            "tax_amount": "0.00",
            "contribution_type_id": "60",
            "api.FinancialItem.get": {
                "is_error": 0,
                "version": 3,
                "count": 1,
                "id": 48868,
                "values": [
                    {
                        "id": "48868",
                        "created_date": "2021-01-20 03:12:24",
                        "transaction_date": "2021-01-20 03:12:23",
                        "contact_id": "49916",
                        "description": "Test Product (National) - Green",
                        "amount": "50.00",
                        "currency": "CAD",
                        "financial_account_id": "90",
                        "status_id": "1",
                        "entity_table": "civicrm_line_item",
                        "entity_id": "122665"
                    }
                ]
            }
        },
        {
            "id": "122666",
            "entity_table": "civicrm_contribution",
            "entity_id": "25960",
            "contribution_id": "25960",
            "price_field_id": "1",
            "label": "PEI T-Shirt",
            "qty": "1.00",
            "unit_price": "68.00",
            "line_total": "68.00",
            "financial_type_id": "60",
            "non_deductible_amount": "0.00",
            "tax_amount": "0.00",
            "contribution_type_id": "60",
            "api.FinancialItem.get": {
                "is_error": 0,
                "version": 3,
                "count": 1,
                "id": 48869,
                "values": [
                    {
                        "id": "48869",
                        "created_date": "2021-01-20 03:12:24",
                        "transaction_date": "2021-01-20 03:12:23",
                        "contact_id": "49916",
                        "description": "PEI T-Shirt",
                        "amount": "18.00",
                        "currency": "CAD",
                        "financial_account_id": "90",
                        "status_id": "1",
                        "entity_table": "civicrm_line_item",
                        "entity_id": "122666"
                    }
                ]
            }
        }
    ]
}

As you can see in the later case it fixes two issues:

  1. There is an associated FinancialItem which was missing earlier.
  2. LineItem total is correct

    Hence, the current patch confirms that it correctly records the financial information on purchasing a product.

monishdeb commented 3 years ago

The current patch fixes another issue mentioned in https://github.com/mecachisenros/woocommerce_civicrm/issues/32 where I the update status code wasn't able to change the Donation's status to 'Cancelled' or 'Refunded' on cancelling or refunding a order. Here's the screenshot of both the cases of:

Cancelled contribution:

Screenshot 2021-01-20 at 1 57 07 PM

Refunded contribution:

Screenshot 2021-01-20 at 2 00 07 PM

Ensured that financial adjustment are correct.

mecachisenros commented 3 years ago

Thanks @agileware-fj for the PRs and @monishdeb for reviewing.

I'll review and test and report back.

JoeMurray commented 3 years ago

@monishdeb could you test for multiple line items to ensure that financial items are created properly in that use case.

Thanks, @mecachisenros for your work on this. JMA would like to leave the work and testing for the membership stuff to you and others as we focus on the financial stuff. Thanks again @agileware-fj for your patches. Cheers,

agileware-justin commented 3 years ago

This and the other two PRs were funded work as part of a WordPress / CiviCRM integration project.

We were planning on releasing these changes at some stage once the project was done, @JoeMurray your recent comment about maintainers and line items was the prompt to bring that plan forward :)

monishdeb commented 3 years ago

@JoeMurray yes I have tested with multiple line-items and posted my findings above. I have tested with two items:

  1. Test Product (National) - Green ($50)
  2. PEI T-Shirt ($18)

And using chain API result, that the line-items and financial items are created correctly.

mecachisenros commented 3 years ago

@monishdeb @agileware-fj @agileware-justin @JoeMurray
Merged, with some fixes:

mecachisenros commented 3 years ago

Forgot to mention, added basic Membership implementation.

The membership is configurable in the CiviCRM Settings panel in the Edit/Add Product screen in Woo.

All membership default/settings (i.e. period, plan, etc.) are based on how the Membership is configured in CiviCRM.

monishdeb commented 3 years ago

Thanks you so much guys for getting this PR merged. Much needed change to get the financial entries recorded for the purchased order. Also enables line-item editor to edit the associated line-items properly now, unlike earlier which throws an error due to missing financial-item, which fixed by this patch.