~magentoerpconnect-core-editors/magentoerpconnect/trunk_version

« back to all changes in this revision

Viewing changes to magentoerpconnect/sale.py

  • Committer: Guewen Baconnier @ Camptocamp
  • Date: 2012-03-15 09:47:04 UTC
  • mfrom: (556 magentoerpconnect)
  • mto: This revision was merged to the branch mainline in revision 571.
  • Revision ID: guewen.baconnier@camptocamp.com-20120315094704-tnmx5uvsqfm12ohl
[MRG] from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
205
205
                    so_obj.write(cr, uid, order.id, {'need_to_update': False})
206
206
                cr.commit();
207
207
        return False
208
 
         
 
208
 
 
209
    def _create_magento_invoice(self, cr, uid, order, conn, ext_id, context=None):
 
210
        """ Creation of an invoice on Magento."""
 
211
        cr.execute("select account_invoice.id "
 
212
                   "from account_invoice "
 
213
                   "inner join sale_order_invoice_rel "
 
214
                   "on invoice_id = account_invoice.id "
 
215
                   "where order_id = %s" % order.id)
 
216
        resultset = cr.fetchone()
 
217
        created = False
 
218
        if resultset and len(resultset) == 1:
 
219
            invoice = self.pool.get("account.invoice").browse(
 
220
                cr, uid, resultset[0], context=context)
 
221
            if (invoice.amount_total == order.amount_total and
 
222
                not invoice.magento_ref):
 
223
                try:
 
224
                    magento_invoice_ref = conn.call(
 
225
                        'sales_order_invoice.create',
 
226
                        [order.magento_incrementid,
 
227
                         [],
 
228
                         _("Invoice Created"),
 
229
                         True,
 
230
                         order.shop_id.allow_magento_notification])
 
231
                    self.pool.get("account.invoice").write(
 
232
                        cr, uid,
 
233
                        invoice.id,
 
234
                        {'magento_ref': magento_invoice_ref,
 
235
                         'origin': magento_invoice_ref})
 
236
                    self.log(cr, uid, order.id,
 
237
                             "created Magento invoice for order %s" %
 
238
                             (order.id,))
 
239
                    created = True
 
240
                except Exception, e:
 
241
                    self.log(cr, uid, order.id,
 
242
                             "failed to create Magento invoice for order %s" %
 
243
                             (order.id,))
 
244
                    # TODO make sure that's because Magento invoice already
 
245
                    # exists and then re-attach it!
 
246
        return created
 
247
 
209
248
    def update_shop_orders(self, cr, uid, order, ext_id, context=None):
210
249
        if context is None: context = {}
211
250
        result = {}
212
251
 
213
 
        if order.shop_id.allow_magento_order_status_push:        
 
252
        if order.shop_id.allow_magento_order_status_push:
214
253
            #status update:
215
254
            conn = context.get('conn_obj', False)
216
 
            logger = netsvc.Logger()
217
255
            status = ORDER_STATUS_MAPPING.get(order.state, False)
218
256
            if status:
219
 
                result['status_change'] = conn.call('sales_order.addComment', [ext_id, status, '', order.shop_id.allow_magento_notification])
220
 
                # If status has changed into OERP and the order need_to_update, then we consider the update is done
 
257
                result['status_change'] = conn.call(
 
258
                    'sales_order.addComment',
 
259
                    [ext_id, status, '',
 
260
                     order.shop_id.allow_magento_notification])
 
261
                # If status has changed into OERP and the order need_to_update,
 
262
                # then we consider the update is done
221
263
                # remove the 'need_to_update': True
222
264
                if order.need_to_update:
223
 
                    self.pool.get('sale.order').write(cr, uid, order.id, {'need_to_update': False})
224
 
        
225
 
            #creation of Magento invoice eventually:
226
 
            cr.execute("select account_invoice.id from account_invoice inner join sale_order_invoice_rel on invoice_id = account_invoice.id where order_id = %s" % order.id)
227
 
            resultset = cr.fetchone()
228
 
            if resultset and len(resultset) == 1:
229
 
                invoice = self.pool.get("account.invoice").browse(cr, uid, resultset[0])
230
 
                if invoice.amount_total == order.amount_total and not invoice.magento_ref:
231
 
                    try:
232
 
                        result['magento_invoice_ref'] = conn.call('sales_order_invoice.create', [order.magento_incrementid, [], _("Invoice Created"), True, order.shop_id.allow_magento_notification])
233
 
                        self.pool.get("account.invoice").write(cr, uid, invoice.id, {'magento_ref': result['magento_invoice_ref'], 'origin': result['magento_invoice_ref']})
234
 
                        self.log(cr, uid, order.id, "created Magento invoice for order %s" % (order.id,))
235
 
                    except Exception, e:
236
 
                        self.log(cr, uid, order.id, "failed to create Magento invoice for order %s" % (order.id,))
237
 
                        logger.notifyChannel('ext synchro', netsvc.LOG_DEBUG, "failed to create Magento invoice for order %s" % (order.id,))
238
 
                        #TODO make sure that's because Magento invoice already exists and then re-attach it!
 
265
                    self.pool.get('sale.order').write(
 
266
                        cr, uid, order.id, {'need_to_update': False})
239
267
 
 
268
            self._create_magento_invoice(
 
269
                cr, uid, order, conn, ext_id, context=context)
240
270
        return result
241
271
 
242
 
 
243
 
 
244
272
    def _sale_shop(self, cr, uid, callback, context=None):
245
273
        if context is None:
246
274
            context = {}