django-magento 1.3.0
Magento integration
License: MIT
Requires Python: >=3.7

# Linets Magento

## Starting

These instructions will allow you to install the library in your django project.

## Current features

1. List orders by filters.
1. Get order detail.
1. Get order products.
1. Create shipment.
1. Create invoice.
1. Update order.
1. Cancel order.

## Pre-requisitos

1. Python >= 3.7
1. requests >= 2
1. Django >= 3,

## Installation

To get the latest stable release from PyPi:

```bash
pip install django-magento
```

or From a build

```bash
git clone https://gitlab.com/linets/ecommerce/oms/integrations/oms-magento
cd {{project}} && git checkout develop
python setup.py sdist
```

and, install in your project Django.

```shell
pip install {{path}}/oms-magento/dist/{{tar.gz file}}
```

### Settings in django project

```python
DJANGO_MAGENTO = {
    'MAGENTO': {
        'BASE_URL': '<MAGENTO_API_URL>',
        'API_KEY': '<MAGENTO_API_KEY>',
    },
}
```

## Usage

```python
from magento.handler import MagentoHandler
handler = MagentoHandler()
```

### List orders in Magento:

Basic list orders.

```python
default_data = handler.get_orders()
```

You can send filter parameters.

```python
params = {
    'searchCriteria[filterGroups][1][filters][0][field]': 'created_at',
    'searchCriteria[filterGroups][1][filters][0][value]': '2021-05-10 12:12:50',
    'searchCriteria[filterGroups][1][filters][0][conditionType]': 'gt',
    'fields': 'items[entity_id,status,state,increment_id],search_criteria'
}

# if params is empty, filter by status will be equal to 'complete'.
default_data = handler.get_orders(params)
```

```python
# Output:
{
    'items': [....],
    'search_criteria': {
        'filter_groups': [
            {
                'filters': [
                    {
                        'field': 'status',
                        'value': 'pending',
                        'condition_type': 'eq'
                    }
                  ]
            },
            {
                'filters': [
                    {
                        'field': 'created_at',
                        'value': '2021-05-14 15:00:37',
                        'condition_type': 'lt'
                    }
                ]
            }
        ]
    },
    'total_count': 0
}
```

Get order detail in Magento:

```python
default_data = handler.get_order_detail(identifier)

# Output:
{
    'base_currency_code': 'CLP',
    'base_discount_amount': 0,
    'base_discount_invoiced': 0,
    'base_grand_total': 31990,
    'base_discount_tax_compensation_amount': 0,
    'base_discount_tax_compensation_invoiced': 0,
    'base_shipping_amount': 0,
    'base_shipping_discount_amount': 0,
    'base_shipping_discount_tax_compensation_amnt': 0,
    'base_shipping_incl_tax': 0,
    'base_shipping_invoiced': 0,
    'base_shipping_tax_amount': 0,
    'base_subtotal': 31990,
    'items': [...],
    'billing_address': {...},
    'payment': {...},
    'status_histories': {...},
    'extension_attributes': {...},
    'payment_additional_info': {...},
    'gift_cards': [...],
    'base_gift_cards_amount': 0,
    'gift_cards_amount': 0,
    'applied_taxes': [...],
    'item_applied_taxes': [...],
    'gw_base_price': '0.0000',
    'gw_price': '0.0000',
    'gw_items_base_price': '0.0000',
    'gw_items_price': '0.0000',
    'gw_card_base_price': '0.0000',
    'gw_card_price': '0.0000',
    'checkout_request_invoice': 'No',
    'rut': '17.716.251-5
}
```

Get products for order:

```python
response = handler.get_order_products(identifier)

# Output:
{
    "items": [
        {
            "item_id": 140,
            "qty_ordered": 1,
            "sku": "ALCNHDA01B57Z"
        }
    ]
}
```

Create shipment:

```python
items = [{'order_item_id': 9999, 'qty': 1}]
response = handler.create_shipment(identifier, items)

Output:
9999
```

Get invoice

```python
from magento.handler import MagentoHandler

handler = MagentoHandler()

response = handler.get_invoice(747118)

Output:
{'items': [{'base_currency_code': 'CLP',
   'base_discount_amount': 0,
   'base_grand_total': 29990,
   'base_discount_tax_compensation_amount': 0,
   'base_shipping_amount': 0,
   'base_shipping_discount_tax_compensation_amnt': 0,
   'base_shipping_incl_tax': 0,
   'base_shipping_tax_amount': 0,
   'base_subtotal': 29990,
   'base_subtotal_incl_tax': 29990,
   'base_tax_amount': 0,
   'base_total_refunded': 29990,
   'base_to_global_rate': 1,
   'base_to_order_rate': 1,
   'billing_address_id': 194,
   'created_at': '2020-10-29 19:06:51',
   'discount_amount': 0,
   'entity_id': 12,
   'global_currency_code': 'CLP',
   'grand_total': 29990,
   'discount_tax_compensation_amount': 0,
   'increment_id': '2000000012',
   'is_used_for_refund': 1,
   'order_currency_code': 'CLP',
   'order_id': 97,
   'shipping_address_id': 193,
   'shipping_amount': 0,
   'shipping_discount_tax_compensation_amount': 0,
   'shipping_incl_tax': 0,
   'shipping_tax_amount': 0,
   'state': 2,
   'store_currency_code': 'CLP',
   'store_id': 2,
   'store_to_base_rate': 0,
   'store_to_order_rate': 0,
   'subtotal': 29990,
   'subtotal_incl_tax': 29990,
   'tax_amount': 0,
   'total_qty': 2,
   'transaction_id': '191101',
   'updated_at': '2021-06-15 15:15:36',
   'items': [{'base_discount_tax_compensation_amount': 0,
     'base_price': 29990,
     'base_price_incl_tax': 29990,
     'base_row_total': 29990,
     'base_row_total_incl_tax': 29990,
     'base_tax_amount': 0,
     'entity_id': 15,
     'discount_tax_compensation_amount': 0,
     'name': 'PANTALÓN LISO',
     'parent_id': 7,
     'price': 29990,
     'price_incl_tax': 29990,
     'product_id': 2411,
     'row_total': 29990,
     'row_total_incl_tax': 29990,
     'sku': '11020205210250',
     'tax_amount': 0,
     'order_item_id': 199,
     'qty': 1},
    {'base_price': 0,
     'entity_id': 16,
     'name': 'PANTALÓN LISO',
     'parent_id': 7,
     'price': 29990,
     'product_id': 2311,
     'sku': '11020205210250',
     'order_item_id': 200,
     'qty': 1}],
   'comments': [],
   'extension_attributes': {'am_giftcard_invoice': {'entity_id': 0,
     'invoice_id': 7,
     'gift_amount': 0,
     'base_gift_amount': 0}}}],
 'search_criteria': {'filter_groups': [{'filters': [{'field': 'order_id',
      'value': '97',
      'condition_type': 'eq'}]}]},
 'total_count': 1}
```

Create invoice:

```python
items = [{'order_item_id': 9999, 'qty': 1}]
response = handler.create_invoice(identifier, items)

# Output:
9999
```

Update order:

```python
response = handler.update_order(identifier, comment, status, notify_customer)

# Output:
True
```

Cancel order in Magento:

```python
response = handler.cancel_order(identifier)

# Output:
True
```

Get item credit memo payload:

```
from magento.handler import MagentoHandler

handler = MagentoHandler()
items = handler.get_item_creditmemo_payload(5, 2)

Output:

{
'order_item_id': 5,
'qty': 2
}

```

Get credit memo default payload:

```
from magento.handler import MagentoHandler

handler = MagentoHandler()
items = [handler.get_item_creditmemo_payload(5, 2)]
data = {
    'items': [
        {
        'order_item_id': 5,
        'qty': 2
        }
    ],
    'notify': True,
    'arguments': {
        'shipping_amount': 1500,
        'adjustment_positive': 2000,
        'adjustment_negative': 0,
        'extension_attributes': {
            'return_to_stock_items': [5, 3]
        },
    },
}
data_instance = json.loads(
            json.dumps(payload), object_hook=lambda attr: SimpleNamespace(**attr)
        )
data = self.magento_handler.get_creditmemo_default_payload(data_instance)

Output:
{
    'items': [
        {
        'order_item_id': 5,
        'qty': 2
        }
    ],
    'notify': True,
    'arguments': {
        'shipping_amount': 1500,
        'adjustment_positive': 2000,
        'adjustment_negative': 0,
        'extension_attributes': {
            'return_to_stock_items': [5, 3]
        },
    },
}
```

Create a credit memo:

```python
from magento.handler import MagentoHandler

handler = MagentoHandler()
invoice = Invoice.objects.get(order=order)

data = {
    'items': [
        {
        'order_item_id': 5,
        'qty': 2
        },
        {
        'order_item_id': 3,
        'qty': 4
        },
    ],
    'notify': True,
    'arguments': {
        'shipping_amount': 1500,
        'adjustment_positive': 2000,
        'adjustment_negative': 0,
        'extension_attributes': {
            'return_to_stock_items': [5,3]
        },
    },
}
response = handler.online_refund(invoice.magento_id, data)

Output:
0
```

Get a credit memo info by id:

```
from magento.handler import MagentoHandler

handler = MagentoHandler()
response = handler.get_creditmemo(11)

Output:
{
  "adjustment": -2500,
  "adjustment_negative": 2500,
  "adjustment_positive": 0,
  "base_adjustment": -2500,
  "base_adjustment_negative": 2500,
  "base_adjustment_positive": 0,
  "base_currency_code": "CLP",
  "base_discount_amount": 0,
  "base_grand_total": 34490,
  "base_discount_tax_compensation_amount": 0,
  "base_shipping_amount": 0,
  "base_shipping_incl_tax": 0,
  "base_shipping_tax_amount": 0,
  "base_subtotal": 36990,
  "base_subtotal_incl_tax": 36990,
  "base_tax_amount": 0,
  "base_to_global_rate": 1,
  "base_to_order_rate": 1,
  "billing_address_id": 344,
  "created_at": "2021-06-18 18:03:59",
  "discount_amount": 0,
  "entity_id": 11,
  "global_currency_code": "CLP",
  "grand_total": 34490,
  "discount_tax_compensation_amount": 0,
  "increment_id": "4000000003",
  "invoice_id": 54,
  "order_currency_code": "CLP",
  "order_id": 172,
  "shipping_address_id": 343,
  "shipping_amount": 0,
  "shipping_incl_tax": 0,
  "shipping_tax_amount": 0,
  "state": 2,
  "store_currency_code": "CLP",
  "store_id": 4,
  "store_to_base_rate": 0,
  "store_to_order_rate": 0,
  "subtotal": 36990,
  "subtotal_incl_tax": 36990,
  "tax_amount": 0,
  "updated_at": "2021-06-18 18:04:01",
  "items": [
    {
      "base_cost": null,
      "base_discount_tax_compensation_amount": 0,
      "base_price": 36990,
      "base_price_incl_tax": 36990,
      "base_row_total": 36990,
      "base_row_total_incl_tax": 36990,
      "base_tax_amount": 0,
      "base_weee_tax_row_disposition": 0,
      "entity_id": 19,
      "discount_tax_compensation_amount": 0,
      "name": "CALZA CON CIERREY PESPUNTES DECORATIVOS.",
      "order_item_id": 365,
      "parent_id": 11,
      "price": 36990,
      "price_incl_tax": 36990,
      "product_id": 144,
      "qty": 1,
      "row_total": 36990,
      "row_total_incl_tax": 36990,
      "sku": "16107107916338",
      "tax_amount": 0,
      "weee_tax_applied": "[]",
      "weee_tax_applied_row_amount": 0,
      "weee_tax_row_disposition": 0
    },
    {
      "base_cost": null,
      "base_price": 0,
      "entity_id": 20,
      "name": "CALZA CON CIERREY PESPUNTES DECORATIVOS.",
      "order_item_id": 366,
      "parent_id": 11,
      "price": 36990,
      "product_id": 146,
      "qty": 1,
      "sku": "16107107916338"
    }
  ],
  "comments": [],
  "extension_attributes": {
    "am_giftcard_creditmemo": {
      "entity_id": 0,
      "creditmemo_id": 11,
      "gift_amount": 0,
      "base_gift_amount": 0
    }
  }
}
```