Skip to content

Commit

Permalink
Merge pull request OCA#5 from simahawk/9.0-mig+tecnativa-imp
Browse files Browse the repository at this point in the history
[WIP] 9.0 mig + tecnativa imp + catalog manager
  • Loading branch information
guewen authored Nov 28, 2016
2 parents af58572 + ad18e90 commit 2fdfde4
Show file tree
Hide file tree
Showing 95 changed files with 3,081 additions and 1,442 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ virtualenv:
install:
- git clone --depth=1 https://github.com/OCA/maintainer-quality-tools.git ${HOME}/maintainer-quality-tools
- export PATH=${HOME}/maintainer-quality-tools/travis:${PATH}
- pip install html2text
- pip install prestapyt
- travis_install_nightly

script:
Expand Down
1 change: 1 addition & 0 deletions connector_prestashop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from . import connector
from . import consumer
from . import models
from . import unit
7 changes: 5 additions & 2 deletions connector_prestashop/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": "9.0.1.0.3",
"license": "AGPL-3",
"depends": [
"base_vat",
"account",
"product",
"product_multi_category", # oca/product-attribute
"connector_ecommerce", # oca/connector-ecommerce
Expand All @@ -22,9 +22,11 @@
],
"external_dependencies": {
'python': [
"unidecode",
"html2text",
"prestapyt",
# tests dependencies
"freezegun",
"vcr",
],
},
"author": "Akretion,"
Expand All @@ -49,6 +51,7 @@
'views/partner_view.xml',
'views/sale_view.xml',
'views/account_view.xml',
'views/stock_view.xml',
'security/ir.model.access.csv',
'security/prestashop_security.xml',
'data/ecommerce_data.xml',
Expand Down
19 changes: 0 additions & 19 deletions connector_prestashop/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,6 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp.addons.connector.connector import ConnectorEnvironment
from openerp.addons.connector.checkpoint import checkpoint


def add_checkpoint(session, model_name, record_id, backend_id):
""" Add a row in the model ``connector.checkpoint`` for a record,
meaning it has to be reviewed by a user.
:param session: current session
:type session: \
:py:class:`openerp.addons.connector.session.ConnectorSession`
:param model_name: name of the model of the record to be reviewed
:type model_name: str
:param record_id: ID of the record to be reviewed
:type record_id: int
:param backend_id: ID of the PrestaShop Backend
:type backend_id: int
"""
return checkpoint.add_checkpoint(session, model_name, record_id,
'prestashop.backend', backend_id)


def get_environment(session, model_name, backend_id):
Expand Down
5 changes: 3 additions & 2 deletions connector_prestashop/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-


# keep this at the top!
from . import binding

from . import account_invoice
Expand All @@ -9,6 +9,7 @@
from . import account_tax_group
from . import delivery_carrier
from . import mail_message
from . import payment
from . import prestashop_backend
from . import product_category
from . import product_image
Expand All @@ -23,6 +24,6 @@
from . import res_partner_category
from . import sale_order
from . import sale_order_state
from . import stock_warehouse
from . import stock_move
from . import stock_tracking
from . import stock_warehouse
65 changes: 25 additions & 40 deletions connector_prestashop/models/account_invoice/common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import models, fields
from openerp import api, fields, models

from ...backend import prestashop
from ...unit.backend_adapter import GenericAdapter
Expand All @@ -12,7 +12,7 @@ class AccountInvoice(models.Model):

prestashop_bind_ids = fields.One2many(
comodel_name='prestashop.refund',
inverse_name='openerp_id',
inverse_name='odoo_id',
string='PrestaShop Bindings'
)

Expand All @@ -25,73 +25,58 @@ def action_move_create(self):
continue
sale_order = sale_order[0]
discount_product_id = sale_order.backend_id.discount_product_id.id

for invoice_line in invoice.invoice_line:
if invoice_line.product_id.id != discount_product_id:
continue
amount = invoice_line.price_subtotal
partner_id = invoice.partner_id.commercial_partner_id.id
refund_id = self._find_refund(-1 * amount, partner_id)
if refund_id:
partner = invoice.partner_id.commercial_partner_id
refund = self._find_refund(-1 * amount, partner)
if refund:
invoice_line.unlink()
line_replacement[invoice.id] = refund_id
invoice.button_reset_taxes()

line_replacement[invoice] = refund
result = super(AccountInvoice, self).action_move_create()
# reconcile invoice with refund
for invoice_id, refund_id in line_replacement.items():
self._reconcile_invoice_refund(invoice_id, refund_id)
for invoice, refund in line_replacement.items():
self._reconcile_invoice_refund(invoice, refund)
return result

def _reconcile_invoice_refund(self, cr, uid, invoice_id, refund_id,
context=None):
move_line_obj = self.pool.get('account.move.line')
invoice_obj = self.pool.get('account.invoice')

invoice = invoice_obj.browse(cr, uid, invoice_id, context=context)
refund = invoice_obj.browse(cr, uid, refund_id, context=context)

move_line_ids = move_line_obj.search(cr, uid, [
@api.model
def _reconcile_invoice_refund(self, invoice, refund):
move_line_obj = self.env['account.move.line']
move_lines = move_line_obj.search([
('move_id', '=', invoice.move_id.id),
('debit', '!=', 0.0),
], context=context)
move_line_ids += move_line_obj.search(cr, uid, [
])
move_lines += move_line_obj.search([
('move_id', '=', refund.move_id.id),
('credit', '!=', 0.0),
], context=context)
move_line_obj.reconcile_partial(
cr, uid, move_line_ids, context=context
)
])
move_lines.reconcile_partial()

def _find_refund(self, cr, uid, amount, partner_id, context=None):
ids = self.search(cr, uid, [
@api.model
def _find_refund(self, amount, partner):
records = self.search([
('amount_untaxed', '=', amount),
('type', '=', 'out_refund'),
('state', '=', 'open'),
('partner_id', '=', partner_id),
('partner_id', '=', partner.id),
])
if not ids:
return None
return ids[0]
return records[:1].id


class PrestashopRefund(models.Model):
_name = 'prestashop.refund'
_inherit = 'prestashop.binding'
_inherits = {'account.invoice': 'openerp_id'}
_inherit = 'prestashop.binding.odoo'
_inherits = {'account.invoice': 'odoo_id'}

openerp_id = fields.Many2one(
odoo_id = fields.Many2one(
comodel_name='account.invoice',
required=True,
ondelete='cascade',
string='Invoice',
oldname='openerp_id',
)

_sql_constraints = [
('prestashop_erp_uniq', 'unique(backend_id, openerp_id)',
'A erp record with same ID on PrestaShop already exists.'),
]


@prestashop
class RefundAdapter(GenericAdapter):
Expand Down
38 changes: 21 additions & 17 deletions connector_prestashop/models/account_invoice/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import_batch,
DelayedBatchImporter,
)
from ...connector import add_checkpoint


@prestashop
Expand All @@ -35,18 +34,15 @@ def _after_import(self, binding):
# FIXME: context should be frozen
context = self.session.context
context['company_id'] = self.backend_record.company_id.id
invoice = binding.openerp_id
# FIXME: this method does not exist
invoice.button_reset_taxes()

invoice = binding.odoo_id

if invoice.amount_total == float(self.prestashop_record['amount']):
invoice.signal_workflow('invoice_open')
else:
add_checkpoint(
self.session,
'account.invoice',
invoice.id,
self.backend_record.id
self.backend_record.add_checkpoint(
model='account.invoice',
record_id=invoice.id,
)


Expand All @@ -69,7 +65,18 @@ def journal(self, record):

def _get_order(self, record):
binder = self.binder_for('prestashop.sale.order')
return binder.to_openerp(record['id_order'])
return binder.to_odoo(record['id_order'])

@mapping
def from_sale_order(self, record):
sale_order = self._get_order(record)
fiscal_position = None
if sale_order.fiscal_position:
fiscal_position = sale_order.fiscal_position_id.id
return {
'origin': sale_order['name'],
'fiscal_position_id': fiscal_position,
}

@mapping
def comment(self, record):
Expand Down Expand Up @@ -132,11 +139,9 @@ def _invoice_line_shipping(self, record, fpos):

def _get_shipping_order_line(self, record):
binder = self.binder_for('prestashop.sale.order')
sale_order = binder.to_openerp(record['id_order'], unwrap=True)

sale_order = binder.to_odoo(record['id_order'], unwrap=True)
if not sale_order.carrier_id:
return None

sale_order_line_ids = self.env['sale.order.line'].search([
('order_id', '=', sale_order.id),
('product_id', '=', sale_order.carrier_id.product_id.id),
Expand Down Expand Up @@ -213,18 +218,17 @@ def type(self, record):
@mapping
def partner_id(self, record):
binder = self.binder_for('prestashop.res.partner')
partner = binder.to_openerp(record['id_customer'], unwrap=True)
partner = binder.to_odoo(record['id_customer'], unwrap=True)
return {'partner_id': partner.id}

@mapping
def account_id(self, record):
binder = self.binder_for('prestashop.sale.order')
binder = self.binder_for('prestashop.res.partner')
partner = binder.to_openerp(record['id_customer'])
partner = binder.to_odoo(record['id_customer'])
partner = partner.with_context(
company_id=self.backend_record.company_id.id,
)
return {'account_id': partner.property_account_receivable.id}
return {'account_id': partner.property_account_receivable_id.id}

@mapping
def company_id(self, record):
Expand Down
2 changes: 1 addition & 1 deletion connector_prestashop/models/account_payment_mode/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PaymentModeBinder(Binder):

_external_field = 'name'

def to_openerp(self, external_id, unwrap=False, company=None):
def to_odoo(self, external_id, unwrap=False, company=None):
if company is None:
company = self.backend_record.company_id
bindings = self.model.with_context(active_test=False).search(
Expand Down
9 changes: 5 additions & 4 deletions connector_prestashop/models/account_payment_mode/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from ...unit.importer import BatchImporter
from ...backend import prestashop
from ...connector import add_checkpoint


@prestashop
Expand All @@ -24,7 +23,7 @@ def _import_record(self, record):
If we have only 1 bank journal, we link the payment method to it,
otherwise, the user will have to create manually the payment mode.
"""
if self.binder_for().to_openerp(record['payment']):
if self.binder_for().to_odoo(record['payment']):
return # already exists
method_xmlid = 'account.account_payment_method_manual_in'
payment_method = self.env.ref(method_xmlid, raise_if_not_found=False)
Expand All @@ -44,5 +43,7 @@ def _import_record(self, record):
'fixed_journal_id': journals.id,
'payment_method_id': payment_method.id
})
add_checkpoint(self.session, self.model._name, mode.id,
self.backend_record.id)
self.backend_record.add_checkpoint(
model=self.model._name,
record_id=mode.id,
)
12 changes: 8 additions & 4 deletions connector_prestashop/models/account_tax/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@

class PrestashopAccountTax(models.Model):
_name = 'prestashop.account.tax'
# Do not inherit from `prestashop.binding.odoo`
# because we do not want the constraint `prestashop_erp_uniq`.
# This allows us to create duplicated taxes.
_inherit = 'prestashop.binding'
_inherits = {'account.tax': 'openerp_id'}
_inherits = {'account.tax': 'odoo_id'}

openerp_id = fields.Many2one(
odoo_id = fields.Many2one(
comodel_name='account.tax',
string='Tax',
required=True,
ondelete='cascade'
ondelete='cascade',
oldname='openerp_id',
)


Expand All @@ -25,7 +29,7 @@ class AccountTax(models.Model):

prestashop_bind_ids = fields.One2many(
comodel_name='prestashop.account.tax',
inverse_name='openerp_id',
inverse_name='odoo_id',
string='prestashop Bindings',
readonly=True,
)
Expand Down
14 changes: 5 additions & 9 deletions connector_prestashop/models/account_tax_group/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AccountTaxGroup(models.Model):

prestashop_bind_ids = fields.One2many(
comodel_name='prestashop.account.tax.group',
inverse_name='openerp_id',
inverse_name='odoo_id',
string='PrestaShop Bindings',
readonly=True
)
Expand All @@ -31,21 +31,17 @@ class AccountTaxGroup(models.Model):

class PrestashopAccountTaxGroup(models.Model):
_name = 'prestashop.account.tax.group'
_inherit = 'prestashop.binding'
_inherits = {'account.tax.group': 'openerp_id'}
_inherit = 'prestashop.binding.odoo'
_inherits = {'account.tax.group': 'odoo_id'}

openerp_id = fields.Many2one(
odoo_id = fields.Many2one(
comodel_name='account.tax.group',
string='Tax Group',
required=True,
ondelete='cascade',
oldname='openerp_id',
)

_sql_constraints = [
('prestashop_erp_uniq', 'unique(backend_id, openerp_id)',
'A erp record with same ID on PrestaShop already exists.'),
]


@prestashop
class TaxGroupAdapter(GenericAdapter):
Expand Down
Loading

0 comments on commit 2fdfde4

Please sign in to comment.