~allison-miller/openerp-connector/7.0-magentoerpconnect-invoice-export-state

« back to all changes in this revision

Viewing changes to magentoerpconnect/invoice.py

  • Committer: Allison Miller
  • Date: 2013-08-05 12:25:03 UTC
  • Revision ID: allison.miller@logicsupply.com-20130805122503-pu1zpzf5tqy7t623
Added Magento store configuration to switch between states on which to
sync an OpenERP invoice to Magento.  Exporter will not run unless invoice
state matches that of the configuration when the event is fired.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from openerp.addons.connector.queue.job import job
27
27
from openerp.addons.connector.unit.synchronizer import ExportSynchronizer
28
28
from openerp.addons.connector.event import on_record_create
29
 
from openerp.addons.connector_ecommerce.event import on_invoice_paid
 
29
from openerp.addons.connector_ecommerce.event import (on_invoice_paid,
 
30
                                                      on_invoice_validated)
30
31
from openerp.addons.connector.exception import IDMissingInBackend
31
32
from .unit.backend_adapter import GenericAdapter
32
33
from .connector import get_environment
160
161
        return item_qty
161
162
 
162
163
    def run(self, binding_id):
163
 
        """ Run the job to export the paid invoice """
 
164
        """ Run the job to export the validated/paid invoice """
164
165
        sess = self.session
165
166
        invoice = sess.browse(self.model._name, binding_id)
166
167
 
202
203
        return invoices[0]['increment_id']
203
204
 
204
205
 
 
206
@on_invoice_validated
205
207
@on_invoice_paid
206
 
def invoice_paid_create_bindings(session, model_name, record_id):
 
208
def invoice_create_bindings(session, model_name, record_id):
207
209
    """
208
210
    Create a ``magento.account.invoice`` record. This record will then
209
211
    be exported to Magento.
213
215
    # we use the shop as many sale orders can be related to an invoice
214
216
    for sale in invoice.sale_ids:
215
217
        for magento_sale in sale.magento_bind_ids:
216
 
            session.create('magento.account.invoice',
217
 
                           {'backend_id': magento_sale.backend_id.id,
218
 
                            'openerp_id': invoice.id,
219
 
                            'magento_order_id': magento_sale.id})
 
218
            # Check if invoice state matches configuration setting
 
219
            # for when to export an invoice
 
220
            magento_stores = magento_sale.shop_id.magento_bind_ids
 
221
            magento_store = next((store for store in magento_stores
 
222
                                  if store.backend_id.id == magento_sale.backend_id.id),
 
223
                                 None)
 
224
            assert magento_store
 
225
            create_invoice = magento_store.create_invoice_on
 
226
 
 
227
            if create_invoice == invoice.state:
 
228
                session.create('magento.account.invoice',
 
229
                               {'backend_id': magento_sale.backend_id.id,
 
230
                                'openerp_id': invoice.id,
 
231
                                'magento_order_id': magento_sale.id})
220
232
 
221
233
 
222
234
@on_record_create(model_names='magento.account.invoice')
224
236
    """
225
237
    Delay the job to export the magento invoice.
226
238
    """
227
 
    export_invoice_paid.delay(session, model_name, record_id)
 
239
    export_invoice_validated_paid.delay(session, model_name, record_id)
228
240
 
229
241
 
230
242
@job
231
 
def export_invoice_paid(session, model_name, record_id):
232
 
    """ Export a paid invoice. """
 
243
def export_invoice_validated_paid(session, model_name, record_id):
 
244
    """ Export a validated or paid invoice. """
233
245
    invoice = session.browse(model_name, record_id)
234
246
    backend_id = invoice.backend_id.id
235
247
    env = get_environment(session, model_name, backend_id)