# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2016-TODAY Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################
from odoo import models, fields, api

class AccountPaymentTerm(models.Model):
	_inherit = "account.payment.term"

	type = fields.Selection([('sale', 'Sale'), ('purchase', 'Purchase')], 'Type')


	@api.model
	def set_payment_terms_type(self):
		old_terms_list = ['account.account_payment_term_15days', 'account.account_payment_term_net', 'account.account_payment_term_immediate']
		terms = []
		for term in old_terms_list:
			res = self.env['ir.model.data'].xmlid_to_res_id(term)
			if res:
				terms.append(res)
		if terms:
			self.browse(terms).write({'type': 'sale'})
		return True


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

	payment_term_id = fields.Many2one('account.payment.term', string='Payment Terms', oldname='payment_term', domain=[('type','=','sale')])
	
class PurchaseOrder(models.Model):
	_inherit = "purchase.order"

	payment_term_id = fields.Many2one('account.payment.term', 'Payment Terms', domain=[('type','=','purchase')])
	