Advertisement
Guest User

Openerp invoice merge

a guest
Mar 15th, 2011
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.96 KB | None | 0 0
  1.     def action_invoice_create(self, cursor, user, ids, journal_id=False,
  2.             group=False, type='out_invoice', context=None):
  3.         '''Return ids of created invoices for the pickings'''
  4.         invoice_obj = self.pool.get('account.invoice')
  5.         invoice_line_obj = self.pool.get('account.invoice.line')
  6.         invoices_group = {}
  7.         res = {}
  8.  
  9.         for picking in self.browse(cursor, user, ids, context=context):
  10.             if picking.invoice_state != '2binvoiced':
  11.                 continue
  12.             payment_term_id = False
  13.             partner = picking.address_id and picking.address_id.partner_id
  14.             if not partner:
  15.                 raise osv.except_osv(_('Error, no partner !'),
  16.                     _('Please put a partner on the picking list if you want to generate invoice.'))
  17.  
  18.             if type in ('out_invoice', 'out_refund'):
  19.                 account_id = partner.property_account_receivable.id
  20.                 payment_term_id = self._get_payment_term(cursor, user, picking)
  21.             else:
  22.                 account_id = partner.property_account_payable.id
  23.  
  24.             address_contact_id, address_invoice_id = \
  25.                     self._get_address_invoice(cursor, user, picking).values()
  26.  
  27.             comment = self._get_comment_invoice(cursor, user, picking)
  28.             if group and partner.id and payment_term_id invoices_group:
  29.                 invoice_id = invoices_group[partner.id]
  30.                 invoice = invoice_obj.browse(cursor, user, invoice_id)
  31.                 invoice_vals = {
  32.                     'name': (invoice.name or '') + ', ' + (picking.name or ''),
  33.                     'origin': (invoice.origin or '') + ', ' + (picking.name or '') + (picking.origin and (':' + picking.origin) or ''),
  34.                     'comment': (comment and (invoice.comment and invoice.comment+"\n"+comment or comment)) or (invoice.comment and invoice.comment or ''),
  35.                 }
  36.                 invoice_obj.write(cursor, user, [invoice_id], invoice_vals, context=context)
  37.             else:
  38.                 invoice_vals = {
  39.                     'name': picking.name,
  40.                     'origin': (picking.name or '') + (picking.origin and (':' + picking.origin) or ''),
  41.                     'type': type,
  42.                     'account_id': account_id,
  43.                     'partner_id': partner.id,
  44.                     'address_invoice_id': address_invoice_id,
  45.                     'address_contact_id': address_contact_id,
  46.                     'comment': comment,
  47.                     'payment_term': payment_term_id,
  48.                     'fiscal_position': partner.property_account_position.id
  49.                     }
  50.                 cur_id = self.get_currency_id(cursor, user, picking)
  51.                 if cur_id:
  52.                     invoice_vals['currency_id'] = cur_id
  53.                 if journal_id:
  54.                     invoice_vals['journal_id'] = journal_id
  55.                 invoice_id = invoice_obj.create(cursor, user, invoice_vals,
  56.                         context=context)
  57.                 invoices_group[partner.id] = invoice_id
  58.             res[picking.id] = invoice_id
  59.             for move_line in picking.move_lines:
  60.                 if move_line.state == 'cancel':
  61.                     continue
  62.                 origin = move_line.picking_id.name
  63.                 if move_line.picking_id.origin:
  64.                     origin += ':' + move_line.picking_id.origin
  65.                 if group:
  66.                     name = (picking.name or '') + '-' + move_line.name
  67.                 else:
  68.                     name = move_line.name
  69.  
  70.                 if type in ('out_invoice', 'out_refund'):
  71.                     account_id = move_line.product_id.product_tmpl_id.\
  72.                             property_account_income.id
  73.                     if not account_id:
  74.                         account_id = move_line.product_id.categ_id.\
  75.                                 property_account_income_categ.id
  76.                 else:
  77.                     account_id = move_line.product_id.product_tmpl_id.\
  78.                             property_account_expense.id
  79.                     if not account_id:
  80.                         account_id = move_line.product_id.categ_id.\
  81.                                 property_account_expense_categ.id
  82.  
  83.                 price_unit = self._get_price_unit_invoice(cursor, user,
  84.                         move_line, type)
  85.                 discount = self._get_discount_invoice(cursor, user, move_line)
  86.                 discount1 = self._get_discount1_invoice(cursor, user, move_line)
  87.                 discount2 = self._get_discount2_invoice(cursor, user, move_line)
  88.                 discount3 = self._get_discount3_invoice(cursor, user, move_line)
  89.                 tax_ids = self._get_taxes_invoice(cursor, user, move_line, type)
  90.                 account_analytic_id = self._get_account_analytic_invoice(cursor,
  91.                         user, picking, move_line)
  92.  
  93.                 #set UoS if it's a sale and the picking doesn't have one
  94.                 uos_id = move_line.product_uos and move_line.product_uos.id or False
  95.                 if not uos_id and type in ('out_invoice', 'out_refund'):
  96.                     uos_id = move_line.product_uom.id
  97.  
  98. #                print picking.origin
  99. #                print picking.name
  100. #                print move_line.customer_origin
  101.  
  102.                 account_id = self.pool.get('account.fiscal.position').map_account(cursor, user, partner.property_account_position, account_id)
  103.                 invoice_line_id = invoice_line_obj.create(cursor, user, {
  104.                     'name': name,
  105.                     'origin': origin,
  106.                     'invoice_id': invoice_id,
  107.                     'uos_id': uos_id,
  108.                     'product_id': move_line.product_id.id,
  109.                     'account_id': account_id,
  110.                     'price_unit': price_unit,
  111.                     'discount': discount,
  112.                     'discount1': discount1,
  113.                     'discount2': discount2,
  114.                     'discount3': discount3,
  115.                     'sale_origin': picking.origin,
  116.                     'ddt_origin': picking.name,
  117.                     'customer_origin': move_line.customer_origin,
  118.                     'quantity': move_line.product_uos_qty or move_line.product_qty,
  119.                     'invoice_line_tax_id': [(6, 0, tax_ids)],
  120.                     'account_analytic_id': account_analytic_id,
  121.                     }, context=context)
  122.                 self._invoice_line_hook(cursor, user, move_line, invoice_line_id)
  123.  
  124.             invoice_obj.button_compute(cursor, user, [invoice_id], context=context,
  125.                     set_total=(type in ('in_invoice', 'in_refund')))
  126.             self.write(cursor, user, [picking.id], {
  127.                 'invoice_state': 'invoiced',
  128.                 }, context=context)
  129.             self._invoice_hook(cursor, user, picking, invoice_id)
  130.         self.write(cursor, user, res.keys(), {
  131.             'invoice_state': 'invoiced',
  132.             }, context=context)
  133.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement