Skip to content

Commit

Permalink
port models and method to api 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yvaucher committed Aug 25, 2014
1 parent ac9a0b7 commit d668e45
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 59 deletions.
18 changes: 8 additions & 10 deletions sale_validity/model/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@
#
#

from openerp.osv import fields, orm
from openerp import models, fields


class res_company(orm.Model):
class ResCompany(models.Model):
_inherit = "res.company"

_columns = {
'default_sale_order_validity_days': fields.integer(
"Default Validity of Sale Orders (in days)",
help="By default, the validity date of sale orders will be "
"the date of the sale order plus the number of days defined "
"in this field. If the value of this field is 0, the sale orders "
"will not have a validity date by default."),
}
default_sale_order_validity_days = fields.Integer(
string="Default Validity of Sale Orders (in days)",
help="By default, the validity date of sale orders will be "
"the date of the sale order plus the number of days defined "
"in this field. If the value of this field is 0, the sale orders "
"will not have a validity date by default.")

_sql_constraints = [
('sale_order_validity_days_positive',
Expand Down
77 changes: 31 additions & 46 deletions sale_validity/model/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,48 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#

from openerp.osv import fields, orm
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
from datetime import datetime
from dateutil.relativedelta import relativedelta

from openerp import models, fields, api


class sale_order(orm.Model):
_inherit = "sale.order"
class SaleOrder(models.Model):
_inherit = 'sale.order'

_columns = {
'date_validity': fields.date(
"Valid Until",
help="Define date until when quotation is valid",
readonly=True,
states={
'draft': [('readonly', False)],
'sent': [('readonly', True)],
},
track_visibility='onchange'),
}
date_validity = fields.Date(
string='Valid Until', readonly=True,
help="Define date until when quotation is valid",
states={
'draft': [('readonly', False)],
'sent': [('readonly', True)],
},
track_visibility='onchange',
default=lambda rec: rec._default_date_validity())

def _default_date_validity(self, cr, uid, context=None):
@api.model
def _default_date_validity(self):
date_validity_str = False
company_id = self.pool['res.company']._company_default_get(
cr, uid, 'sale.order', context=context)
company = self.pool['res.company'].browse(
cr, uid, company_id, context=context)
company_pool = self.env['res.company']
company_id = company_pool._company_default_get('sale.order')
company = company_pool.browse(company_id)
if company.default_sale_order_validity_days:
today_str = fields.date.context_today(
self, cr, uid, context=context)
today = datetime.strptime(today_str, DEFAULT_SERVER_DATE_FORMAT)
today_str = fields.Date.context_today(self)
today = fields.Date.from_string(today_str)
date_validity = today + relativedelta(
days=company.default_sale_order_validity_days)
date_validity_str = date_validity.strftime(
DEFAULT_SERVER_DATE_FORMAT)
date_validity_str = fields.Date.to_string(date_validity)
return date_validity_str

_defaults = {
'date_validity': _default_date_validity,
}

def date_order_change(
self, cr, uid, ids, date_order, date_validity, company_id,
context=None):
res = {'value': {}}
if date_order:
if not company_id:
company_id = self.pool['res.company']._company_default_get(
cr, uid, 'sale.order', context=context)
company = self.pool['res.company'].browse(
cr, uid, company_id, context=context)
@api.onchange('date_order')
def _onchange_date_order(self):
company_pool = self.env['res.company']
if self.date_order:
company = self.company_id
if not company:
company_id = company_pool._company_default_get('sale.order')
company = company_pool.browse(company_id)
if company.default_sale_order_validity_days:
date_order = datetime.strptime(
date_order, DEFAULT_SERVER_DATE_FORMAT)
date_order = fields.Datetime.from_string(self.date_order)
date_validity = date_order + relativedelta(
days=company.default_sale_order_validity_days)
res['value']['date_validity'] = date_validity.strftime(
DEFAULT_SERVER_DATE_FORMAT)
return res
self.date_validity = fields.Date.to_string(date_validity)
3 changes: 0 additions & 3 deletions sale_validity/view/sale_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
<field name="date_order" position="after">
<field name="date_validity"/>
</field>
<field name="date_order" position="attributes">
<attribute name="on_change">date_order_change(date_order, date_validity, company_id, context)</attribute>
</field>
</field>
</record>

Expand Down

0 comments on commit d668e45

Please sign in to comment.