123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- # -*- coding: utf-8 -*-
- ##############################################################################
- # For copyright and license notices, see __openerp__.py file in module root
- # directory
- ##############################################################################
- from dateutil.relativedelta import relativedelta
- import datetime
- import logging
- import time
- from openerp.osv import osv, fields
- import openerp.tools
- from openerp.tools.translate import _
- from openerp import models, api
- from openerp.addons.decimal_precision import decimal_precision as dp
- class account_analytic_account(osv.osv):
- _inherit = "account.analytic.account"
- def _prepare_invoice_data(self, cr, uid, contract, context=None):
- context = context or {}
- journal_obj = self.pool.get('account.journal')
- if contract.type == 'purchase_contract':
- invoice = {}
- if not contract.partner_id:
- raise osv.except_osv(_('No Supplier Defined!'), _(
- "You must first select a Supplier for Contract %s!") % contract.name)
- fpos = contract.partner_id.property_account_position or False
- journal_ids = journal_obj.search(cr, uid, [(
- 'type', '=', 'purchase'), ('company_id', '=', contract.company_id.id or False)], limit=1)
- if not journal_ids:
- raise osv.except_osv(_('Error!'),
- _('Please define a pruchase journal for the company "%s".') % (contract.company_id.name or '', ))
- currency_id = False
- if contract.pricelist_id:
- currency_id = contract.pricelist_id.currency_id.id
- elif contract.partner_id.property_product_pricelist:
- currency_id = contract.partner_id.property_product_pricelist.currency_id.id
- elif contract.company_id:
- currency_id = contract.company_id.currency_id.id
- invoice = {
- 'account_id': contract.partner_id.property_account_payable.id,
- 'type': 'in_invoice',
- 'reference': contract.name,
- 'partner_id': contract.partner_id.id,
- 'currency_id': currency_id,
- 'journal_id': len(journal_ids) and journal_ids[0] or False,
- 'date_invoice': contract.recurring_next_date,
- 'origin': contract.code,
- 'fiscal_position': fpos and fpos.id,
- 'company_id': contract.company_id.id or False,
- }
- return invoice
- else:
- return super(account_analytic_account, self)._prepare_invoice_data(cr, uid, contract, context=context)
- def _prepare_invoice_lines(self, cr, uid, contract, fiscal_position_id, context=None):
- if not context:
- context = {}
- if contract.type == 'purchase_contract':
- fpos_obj = self.pool.get('account.fiscal.position')
- fiscal_position = None
- if fiscal_position_id:
- fiscal_position = fpos_obj.browse(
- cr, uid, fiscal_position_id, context=context)
- invoice_lines = []
- for line in contract.recurring_invoice_line_ids:
- res = line.product_id
- account_id = res.property_account_expense.id
- if not account_id:
- account_id = res.categ_id.property_account_expense_categ.id
- account_id = fpos_obj.map_account(
- cr, uid, fiscal_position, account_id)
- taxes = res.supplier_taxes_id or False
- if taxes:
- tax_ids = [x.id for x in taxes]
- tax_ids = self.pool['account.tax'].search(
- cr, uid,
- [('id', 'in', tax_ids),
- ('company_id', '=', contract.company_id.id)],
- context=context)
- taxes = self.pool['account.tax'].browse(
- cr, uid, tax_ids, context=context)
- tax_id = fpos_obj.map_tax(cr, uid, fiscal_position, taxes)
- invoice_lines.append((0, 0, {
- 'name': line.name,
- 'account_id': account_id,
- 'account_analytic_id': contract.id,
- 'price_unit': line.price_unit or 0.0,
- 'quantity': line.quantity,
- 'uos_id': line.uom_id.id or False,
- 'product_id': line.product_id.id or False,
- 'invoice_line_tax_id': [(6, 0, tax_id)],
- }))
- return invoice_lines
- else:
- return super(account_analytic_account, self)._prepare_invoice_lines(cr, uid, contract, fiscal_position_id, context=None)
- def _cron_recurring_create_invoice_purchase(self, cr, uid, context=None):
- current_date = time.strftime('%Y-%m-%d')
- contract_ids = self.search(cr, uid, [('recurring_next_date', '<=', current_date), (
- 'state', '=', 'open'), ('recurring_invoices', '=', True), ('type', '=', 'purchase_contract')])
- return self._recurring_create_invoice(cr, uid, contract_ids, context=context)
- class AccountAnalyticInvoiceLine(models.Model):
- _inherit = "account.analytic.invoice.line"
- @api.multi
- def product_id_change(
- self, product, uom_id, qty=0, name='', partner_id=False,
- price_unit=False, pricelist_id=False, company_id=None):
- context = self._context or {}
- company_id = company_id or False
- local_context = dict(context, company_id=company_id,
- force_company=company_id, pricelist=pricelist_id)
- result = super(AccountAnalyticInvoiceLine, self).product_id_change(
- product=product, uom_id=uom_id, qty=0, name='',
- partner_id=False, price_unit=False, pricelist_id=False,
- company_id=None)
- if not product or not context.get('purchase', False):
- return result
- res = self.env['product.product'].with_context(local_context).browse(
- product)
- if pricelist_id:
- price = res.price
- else:
- price = res.standard_price
- result['value']['price_unit'] = price
- if result['value']['uom_id'] != res.uom_id.id:
- new_price = self.env['product.uom']._compute_price(
- res.uom_id.id,
- result['value']['price_unit'],
- result['value']['uom_id'])
- result['value']['price_unit'] = new_price
- return result
|