2019-12-04 19:38:00 +05:00
|
|
|
import datetime
|
2019-11-12 16:16:14 +05:00
|
|
|
|
2019-12-25 18:10:48 +05:00
|
|
|
from odoo import _, api, fields, models
|
2019-11-12 16:16:14 +05:00
|
|
|
|
2019-12-28 14:15:12 +05:00
|
|
|
from ..utils import MODULE_NAME
|
2020-01-17 17:22:56 +05:00
|
|
|
from ..utils.misc import Extension, IDocument
|
2019-12-28 14:15:12 +05:00
|
|
|
|
2019-11-12 16:16:14 +05:00
|
|
|
|
2020-01-17 17:22:56 +05:00
|
|
|
class PartnerContract(models.Model, IDocument, Extension):
|
2019-12-17 09:53:47 +05:00
|
|
|
_name = "res.partner.contract"
|
2019-12-23 09:03:39 +05:00
|
|
|
_description = "Contract"
|
2019-12-17 09:53:47 +05:00
|
|
|
_inherit = ["mail.thread", "mail.activity.mixin", "mail.followers"]
|
2019-11-12 16:16:14 +05:00
|
|
|
|
2019-12-23 11:39:53 +05:00
|
|
|
def _get_default_name(self):
|
|
|
|
"""Returns name format `№YYMM-D-N`,
|
|
|
|
where N is a sequence number of contracts which are created this day
|
|
|
|
"""
|
|
|
|
current_day_ts = (
|
|
|
|
datetime.datetime.now()
|
|
|
|
.replace(minute=0, hour=0, second=0, microsecond=0)
|
|
|
|
.timestamp()
|
|
|
|
)
|
|
|
|
|
2020-09-24 00:02:12 +05:00
|
|
|
contracts_today = self.search([("create_date_ts", ">=", current_day_ts)])
|
2019-12-23 11:39:53 +05:00
|
|
|
|
|
|
|
contract_date = "{format_date}-{number}".format(
|
|
|
|
format_date=datetime.date.strftime(datetime.date.today(), "%y%m-%d"),
|
|
|
|
number=len(contracts_today) + 1,
|
|
|
|
)
|
2020-09-24 00:02:12 +05:00
|
|
|
|
2019-12-23 11:39:53 +05:00
|
|
|
return contract_date
|
|
|
|
|
|
|
|
def _get_default_create_date_ts(self):
|
|
|
|
"""Returns timestamp of now by local datetime"""
|
|
|
|
return datetime.datetime.now().timestamp()
|
|
|
|
|
2020-02-05 11:20:17 +05:00
|
|
|
partner_id = fields.Many2one(
|
|
|
|
"res.partner",
|
|
|
|
string="Partner",
|
|
|
|
default=lambda self: self.env.context.get("active_id"),
|
|
|
|
required=True,
|
|
|
|
)
|
|
|
|
company_id = fields.Many2one(
|
|
|
|
"res.partner",
|
|
|
|
string="Company",
|
|
|
|
default=lambda self: self.env.user.company_id.partner_id,
|
|
|
|
)
|
|
|
|
create_date_ts = fields.Char(default=_get_default_create_date_ts)
|
2020-01-09 16:58:54 +05:00
|
|
|
res_model = fields.Char(default=lambda self: self._name)
|
2020-09-25 12:53:44 +05:00
|
|
|
name = fields.Char(
|
|
|
|
string="Contract number",
|
|
|
|
default=_get_default_name,
|
|
|
|
)
|
2019-12-30 10:26:13 +05:00
|
|
|
create_date = fields.Datetime(string="Created on")
|
2020-09-25 12:53:44 +05:00
|
|
|
date_conclusion = fields.Date(
|
|
|
|
string="Date of system conclusion",
|
|
|
|
)
|
2019-12-12 19:29:23 +05:00
|
|
|
date_conclusion_fix = fields.Date(
|
2019-12-23 09:02:11 +05:00
|
|
|
string="Date of manual conclusion",
|
2019-12-17 09:53:47 +05:00
|
|
|
help="Field for manual edit when contract is signed or closed",
|
2019-12-12 19:29:23 +05:00
|
|
|
default=lambda self: self.date_conclusion,
|
|
|
|
)
|
2019-12-17 13:17:58 +05:00
|
|
|
contract_annex_ids = fields.One2many(
|
|
|
|
"res.partner.contract.annex",
|
|
|
|
"contract_id",
|
|
|
|
string="Annexes",
|
|
|
|
help="Annexes to this contract",
|
2019-11-12 16:16:14 +05:00
|
|
|
)
|
2019-12-19 12:33:21 +05:00
|
|
|
contract_annex_number = fields.Integer(
|
|
|
|
default=1, help="Counter for generate Annex name"
|
|
|
|
)
|
2019-12-17 09:53:47 +05:00
|
|
|
state = fields.Selection(
|
2020-09-25 12:53:44 +05:00
|
|
|
[
|
|
|
|
("draft", "New"),
|
|
|
|
("sign", "Signed"),
|
|
|
|
("close", "Closed"),
|
|
|
|
],
|
2019-12-17 09:53:47 +05:00
|
|
|
string="Status",
|
2019-12-12 19:03:09 +05:00
|
|
|
readonly=True,
|
|
|
|
copy=False,
|
|
|
|
index=True,
|
2019-12-17 09:53:47 +05:00
|
|
|
track_visibility="onchange",
|
2019-12-12 19:03:09 +05:00
|
|
|
track_sequence=3,
|
2019-12-17 09:53:47 +05:00
|
|
|
default="draft",
|
2019-12-12 19:03:09 +05:00
|
|
|
)
|
2019-11-12 16:16:14 +05:00
|
|
|
|
2019-12-12 19:03:09 +05:00
|
|
|
@api.multi
|
|
|
|
def action_sign(self):
|
2019-12-17 09:53:47 +05:00
|
|
|
self.write({"state": "sign", "date_conclusion": fields.Date.today()})
|
2019-12-12 19:03:09 +05:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def action_close(self):
|
2019-12-17 09:53:47 +05:00
|
|
|
self.write({"state": "close"})
|
2019-12-12 19:03:09 +05:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def action_renew(self):
|
2019-12-17 09:53:47 +05:00
|
|
|
self.write({"state": "draft"})
|
2019-12-12 19:03:09 +05:00
|
|
|
|
2019-12-18 16:25:01 +05:00
|
|
|
@api.multi
|
|
|
|
def action_print_form(self):
|
2019-12-30 16:02:07 +05:00
|
|
|
view = self.env.ref(
|
2020-01-14 09:47:03 +05:00
|
|
|
"{}.res_partner_wizard_print_document_view".format(MODULE_NAME)
|
2019-12-30 16:02:07 +05:00
|
|
|
)
|
2019-12-18 16:25:01 +05:00
|
|
|
return {
|
2019-12-25 13:04:26 +05:00
|
|
|
"name": _("Print Form of Contract"),
|
2019-12-18 16:25:01 +05:00
|
|
|
"type": "ir.actions.act_window",
|
|
|
|
"res_model": "res.partner.contract.wizard",
|
|
|
|
"view_mode": "form",
|
|
|
|
"view_id": view.id,
|
|
|
|
"target": "new",
|
2020-07-09 01:19:51 +05:00
|
|
|
"context": {
|
|
|
|
"self_id": self.id,
|
|
|
|
"active_model": self._name,
|
2020-07-09 01:20:03 +05:00
|
|
|
"company_form": self.partner_id.company_form
|
|
|
|
if self.partner_id.is_company
|
|
|
|
else "person",
|
|
|
|
},
|
|
|
|
}
|
2019-12-18 16:25:01 +05:00
|
|
|
|
2020-01-17 17:13:45 +05:00
|
|
|
def get_name_by_document_template(self, document_template_id):
|
|
|
|
return self.name
|
|
|
|
|
2020-01-17 16:54:33 +05:00
|
|
|
def get_filename_by_document_template(self, document_template_id):
|
|
|
|
return _("{type} {number} from {date}").format(
|
2020-01-20 18:48:12 +05:00
|
|
|
type=_(
|
|
|
|
dict(document_template_id._fields["document_type"].selection).get(
|
|
|
|
document_template_id.document_type
|
|
|
|
)
|
|
|
|
),
|
2020-01-17 16:54:33 +05:00
|
|
|
number=self.name,
|
|
|
|
date=self.get_date().strftime("%d.%m.%Y"),
|
|
|
|
)
|
|
|
|
|
2019-12-27 19:18:37 +05:00
|
|
|
def get_date(self):
|
2019-12-28 11:04:25 +05:00
|
|
|
"""Uses in xml action (data/fields_default)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
datetime.datetime -- date_conclusion_fix or date_conclusion or create_date
|
|
|
|
"""
|
2019-12-27 19:18:37 +05:00
|
|
|
date = self.date_conclusion_fix or self.date_conclusion
|
|
|
|
if date:
|
2020-01-22 16:46:56 +05:00
|
|
|
date = self.parse_odoo_date(date)
|
2019-12-27 19:18:37 +05:00
|
|
|
else:
|
2020-01-22 16:46:56 +05:00
|
|
|
date = self.parse_odoo_datetime(self.create_date)
|
2019-12-27 19:18:37 +05:00
|
|
|
return date
|
2019-12-27 18:42:46 +05:00
|
|
|
|
2019-12-30 15:42:46 +05:00
|
|
|
def _(self, arg):
|
|
|
|
"""Uses in xml action (data/fields_default)
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
arg {str} -- String to translate
|
|
|
|
"""
|
|
|
|
return _(arg)
|