You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

775 lines
34 KiB

5 years ago
[ADD] bond and loan issues management [ADD] bond and loan issues management module skeleton [IMP] change increase menu sequence [IMP] add models, fields and views [IMP] add xml declaration in head of file [ADD] add easy_my_coop_loan_website - WIP [IMP] add access rights [IMP] this raise inconsistency so replace id by default_code. [IMP] change import openerp to odoo [IMP] add website loan module [FIX] put website display in loan [FIX] fix import [FIX] fix function [IMP] use correct name [IMP] make the loan and bond visible [IMP] add js, field and logic to set amount limit per subscription [IMP] remove dependency on recaptcha as user is logged to subscribe [IMP] add fields [IMP] save loan issue subscription still in WIP [IMP] remove alert pop up [IMP] add dependency to easy_my_coop_website [IMP] remove force send for sub request creation email notification [IMP] add mail templates [IMP] save subscription in the corresponding loan issue. add email notif [FIX] fix loan issue line view [FIX] add related field to loan issue. It is where the data stand [IMP] move term_view up [FIX] fix js error when false is returned [FIX] fix function when loan_issue_id in None [IMP] add actions and button [FIX] fix action [FIX] fix mail template [IMP] set noupdate=1 [IMP] change order [IMP] display loan issue lines on partner form [IMP] add loan view in partner form [IMP] add face value on loan issue and line add face value on loan issue and line. add as well the quantity and computation of the amount [FIX] missing id overriding values wasn't working [IMP] getting bond face value and setting it as step. [IMP] subscribed_amount computed field is the sum of the amount lines [IMP] allow a waiting payment to be cancelled [IMP] make field required [REFACT] move loan issue line code to dedicated file [IMP] add interest calculation and model [ADD] bond and loan issues management module skeleton [IMP] add models, fields and views [IMP] allow creation by hand [IMP] adding partner related field [IMP] put code in separate function [FIX] pass it to get method [IMP] routes consistent form [FIX] fix eof [FIX] GET is working for ajax call [IMP] website page for loan issue subscription
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 Coop IT Easy SCRL fs
  3. # Houssine Bakkali <houssine@coopiteasy.be>
  4. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  5. from datetime import datetime
  6. from odoo import api, fields, models, _
  7. from addons.base_iban.models.res_partner_bank import validate_iban
  8. from odoo.exceptions import UserError, ValidationError
  9. _REQUIRED = ['email',
  10. 'firstname',
  11. 'lastname',
  12. 'birthdate',
  13. 'address',
  14. 'share_product_id',
  15. 'ordered_parts',
  16. 'zip_code',
  17. 'city',
  18. 'iban',
  19. 'gender']
  20. @api.model
  21. def _lang_get(self):
  22. languages = self.env['res.lang'].search([])
  23. return [(language.code, language.name) for language in languages]
  24. # todo move to subscription_request.py
  25. class SubscriptionRequest(models.Model):
  26. _name = 'subscription.request'
  27. _description = 'Subscription Request'
  28. def get_required_field(self):
  29. required_fields = _REQUIRED
  30. company = self.env['res.company']._company_default_get()
  31. if company.data_policy_approval_required:
  32. required_fields.append('data_policy_approved')
  33. if company.internal_rules_approval_required:
  34. required_fields.append('internal_rules_approved')
  35. return required_fields
  36. def get_mail_template_notif(self, is_company=False):
  37. if is_company:
  38. mail_template = 'easy_my_coop.email_template_confirmation_company'
  39. else:
  40. mail_template = 'easy_my_coop.email_template_confirmation'
  41. return self.env.ref(mail_template, False)
  42. def is_member(self, vals, cooperator):
  43. if cooperator.member:
  44. vals['type'] = 'increase'
  45. vals['already_cooperator'] = True
  46. return vals
  47. @api.model
  48. def create(self, vals):
  49. partner_obj = self.env['res.partner']
  50. if not vals.get('partner_id'):
  51. cooperator = False
  52. if vals.get('email'):
  53. cooperator = partner_obj.get_cooperator_from_email(
  54. vals.get('email'))
  55. if cooperator:
  56. # TODO remove the following line once it has
  57. # been found a way to avoid double encoding
  58. cooperator = cooperator[0]
  59. vals['type'] = 'subscription'
  60. vals = self.is_member(vals, cooperator)
  61. vals['partner_id'] = cooperator.id
  62. else:
  63. cooperator_id = vals.get('partner_id')
  64. cooperator = partner_obj.browse(cooperator_id)
  65. vals = self.is_member(vals, cooperator)
  66. if not cooperator.cooperator:
  67. cooperator.write({'cooperator': True})
  68. subscr_request = super(SubscriptionRequest, self).create(vals)
  69. mail_template_notif = subscr_request.get_mail_template_notif(False)
  70. mail_template_notif.send_mail(subscr_request.id)
  71. return subscr_request
  72. @api.model
  73. def create_comp_sub_req(self, vals):
  74. vals["name"] = vals['company_name']
  75. if not vals.get('partner_id'):
  76. cooperator = self.env['res.partner'].get_cooperator_from_crn(vals.get('company_register_number'))
  77. if cooperator:
  78. vals['partner_id'] = cooperator.id
  79. vals['type'] = 'increase'
  80. vals['already_cooperator'] = True
  81. subscr_request = super(SubscriptionRequest, self).create(vals)
  82. confirmation_mail_template = subscr_request.get_mail_template_notif(True)
  83. confirmation_mail_template.send_mail(subscr_request.id)
  84. return subscr_request
  85. def check_empty_string(self, value):
  86. if value is None or value is False or value == '':
  87. return False
  88. return True
  89. def check_iban(self, iban):
  90. try:
  91. if iban:
  92. validate_iban(iban)
  93. return True
  94. else:
  95. return False
  96. except ValidationError:
  97. return False
  98. @api.multi
  99. @api.depends('iban', 'skip_control_ng')
  100. def _validated_lines(self):
  101. for sub_request in self:
  102. validated = (
  103. sub_request.skip_control_ng
  104. or self.check_iban(sub_request.iban)
  105. )
  106. sub_request.validated = validated
  107. @api.multi
  108. @api.depends('share_product_id',
  109. 'share_product_id.list_price',
  110. 'ordered_parts')
  111. def _compute_subscription_amount(self):
  112. for sub_request in self:
  113. sub_request.subscription_amount = (sub_request.share_product_id.
  114. list_price *
  115. sub_request.ordered_parts)
  116. already_cooperator = fields.Boolean(string="I'm already cooperator",
  117. readonly=True,
  118. states={'draft': [('readonly', False)]}
  119. )
  120. name = fields.Char(string='Name',
  121. required=True,
  122. readonly=True,
  123. states={'draft': [('readonly', False)]})
  124. firstname = fields.Char(string='Firstname',
  125. readonly=True,
  126. states={'draft': [('readonly', False)]})
  127. lastname = fields.Char(string='Lastname',
  128. readonly=True,
  129. states={'draft': [('readonly', False)]})
  130. birthdate = fields.Date(string="Birthdate",
  131. readonly=True,
  132. states={'draft': [('readonly', False)]})
  133. gender = fields.Selection([('male', _('Male')),
  134. ('female', _('Female')),
  135. ('other', _('Other'))],
  136. string='Gender',
  137. readonly=True,
  138. states={'draft': [('readonly', False)]})
  139. type = fields.Selection([('new', 'New Cooperator'),
  140. ('subscription', 'Subscription'),
  141. ('increase', 'Increase number of share')],
  142. string='Type', default="new",
  143. readonly=True,
  144. states={'draft': [('readonly', False)]})
  145. state = fields.Selection([('draft', 'Draft'),
  146. ('block', 'Blocked'),
  147. ('done', 'Done'),
  148. ('waiting', 'Waiting'),
  149. ('transfer', 'Transfer'),
  150. ('cancelled', 'Cancelled'),
  151. ('paid', 'paid')],
  152. string='State', required=True, default="draft")
  153. email = fields.Char(string='Email',
  154. required=True,
  155. readonly=True,
  156. states={'draft': [('readonly', False)]})
  157. iban = fields.Char(string='Account Number',
  158. readonly=True,
  159. states={'draft': [('readonly', False)]})
  160. partner_id = fields.Many2one('res.partner',
  161. string='Cooperator',
  162. readonly=True,
  163. states={'draft': [('readonly', False)]})
  164. share_product_id = fields.Many2one('product.product',
  165. string='Share type',
  166. domain=[('is_share', '=', True)],
  167. required=True,
  168. readonly=True,
  169. states={'draft': [('readonly', False)]})
  170. share_short_name = fields.Char(related='share_product_id.short_name',
  171. string='Share type name',
  172. readonly=True,
  173. states={'draft': [('readonly', False)]})
  174. share_unit_price = fields.Float(related='share_product_id.list_price',
  175. string='Share price',
  176. readonly=True,
  177. states={'draft': [('readonly', False)]})
  178. subscription_amount = fields.Monetary(
  179. compute='_compute_subscription_amount',
  180. string='Subscription amount',
  181. currency_field="company_currency_id",
  182. readonly=True,
  183. states={'draft': [('readonly', False)]},
  184. )
  185. ordered_parts = fields.Integer(string='Number of Share',
  186. required=True,
  187. readonly=True,
  188. default=1,
  189. states={'draft': [('readonly', False)]})
  190. address = fields.Char(string='Address',
  191. required=True,
  192. readonly=True,
  193. states={'draft': [('readonly', False)]})
  194. city = fields.Char(string='City',
  195. required=True,
  196. readonly=True,
  197. states={'draft': [('readonly', False)]})
  198. zip_code = fields.Char(string='Zip Code',
  199. required=True,
  200. readonly=True,
  201. states={'draft': [('readonly', False)]})
  202. country_id = fields.Many2one('res.country',
  203. string='Country',
  204. ondelete='restrict',
  205. required=True,
  206. readonly=True,
  207. states={'draft': [('readonly', False)]})
  208. phone = fields.Char(string='Phone',
  209. readonly=True,
  210. states={'draft': [('readonly', False)]})
  211. user_id = fields.Many2one('res.users',
  212. string='Responsible',
  213. readonly=True)
  214. validated = fields.Boolean(compute='_validated_lines',
  215. string='Valid Line?',
  216. readonly=True)
  217. skip_control_ng = fields.Boolean(string="Skip control",
  218. help="if this field is checked then no"
  219. " control will be done on the national"
  220. " register number and on the iban bank"
  221. " account. To be done in case of the id"
  222. " card is from abroad or in case of"
  223. " a passport")
  224. lang = fields.Selection(_lang_get,
  225. string='Language',
  226. required=True,
  227. readonly=True,
  228. states={'draft': [('readonly', False)]},
  229. default=lambda self: self.env['res.company']._company_default_get().default_lang_id.code)
  230. date = fields.Date(string='Subscription date request',
  231. required=True,
  232. readonly=True,
  233. states={'draft': [('readonly', False)]},
  234. default=lambda self: datetime.strftime(datetime.now(),
  235. '%Y-%m-%d'))
  236. company_id = fields.Many2one('res.company',
  237. string='Company',
  238. required=True,
  239. change_default=True,
  240. readonly=True,
  241. default=lambda self: self.env['res.company']._company_default_get())
  242. company_currency_id = fields.Many2one(
  243. "res.currency",
  244. related="company_id.currency_id",
  245. string="Company Currency",
  246. readonly=True,
  247. )
  248. is_company = fields.Boolean(string='Is a company',
  249. readonly=True,
  250. states={'draft': [('readonly', False)]})
  251. is_operation = fields.Boolean(string='Is an operation',
  252. readonly=True,
  253. states={'draft': [('readonly', False)]})
  254. company_name = fields.Char(string="Company name",
  255. readonly=True,
  256. states={'draft': [('readonly', False)]})
  257. company_email = fields.Char(string="Company email",
  258. readonly=True,
  259. states={'draft': [('readonly', False)]})
  260. company_register_number = fields.Char(string='Company register number',
  261. readonly=True,
  262. states={'draft': [('readonly', False)]})
  263. company_type = fields.Selection([('', '')],
  264. string="Company type",
  265. readonly=True,
  266. states={'draft': [('readonly', False)]})
  267. same_address = fields.Boolean(string='Same address',
  268. readonly=True,
  269. states={'draft': [('readonly', False)]})
  270. activities_address = fields.Char(string='Activities address',
  271. readonly=True,
  272. states={'draft': [('readonly', False)]})
  273. activities_city = fields.Char(string='Activities city',
  274. readonly=True,
  275. states={'draft': [('readonly', False)]})
  276. activities_zip_code = fields.Char(string='Activities zip Code',
  277. readonly=True,
  278. states={'draft': [('readonly', False)]})
  279. activities_country_id = fields.Many2one('res.country',
  280. string='Activities country',
  281. ondelete='restrict',
  282. readonly=True,
  283. states={'draft': [('readonly', False)]})
  284. contact_person_function = fields.Char(string='Function',
  285. readonly=True,
  286. states={'draft': [('readonly', False)]})
  287. operation_request_id = fields.Many2one('operation.request',
  288. string="Operation Request",
  289. readonly=True,
  290. states={'draft': [('readonly', False)]})
  291. capital_release_request = fields.One2many('account.invoice',
  292. 'subscription_request',
  293. string='Capital release request',
  294. readonly=True,
  295. states={'draft': [('readonly', False)]})
  296. capital_release_request_date = fields.Date(string="Force the capital "
  297. "release request date",
  298. help="Keep empty to use the "
  299. "current date",
  300. copy=False,
  301. readonly=True,
  302. states={'draft': [('readonly', False)]})
  303. source = fields.Selection([('website', 'Website'),
  304. ('crm', 'CRM'),
  305. ('manual', 'Manual'),
  306. ('operation', 'Operation')],
  307. string="Source",
  308. default="website",
  309. readonly=True,
  310. states={'draft': [('readonly', False)]})
  311. data_policy_approved = fields.Boolean(
  312. string='Data Policy Approved',
  313. default=False,
  314. )
  315. internal_rules_approved = fields.Boolean(
  316. string='Approved Internal Rules',
  317. default=False,
  318. )
  319. _order = "id desc"
  320. def get_person_info(self, partner):
  321. self.firstname = partner.firstname
  322. self.name = partner.name
  323. self.lastname = partner.lastname
  324. self.email = partner.email
  325. self.birthdate = partner.birthdate_date
  326. self.gender = partner.gender
  327. self.address = partner.street
  328. self.city = partner.city
  329. self.zip_code = partner.zip
  330. self.country_id = partner.country_id
  331. self.phone = partner.phone
  332. self.lang = partner.lang
  333. @api.onchange('partner_id')
  334. def onchange_partner(self):
  335. partner = self.partner_id
  336. if partner:
  337. self.is_company = partner.is_company
  338. self.already_cooperator = partner.member
  339. if partner.bank_ids:
  340. self.iban = partner.bank_ids[0].acc_number
  341. if partner.member:
  342. self.type = 'increase'
  343. if partner.is_company:
  344. self.company_name = partner.name
  345. self.company_email = partner.email
  346. self.company_register_number = partner.company_register_number
  347. representative = partner.get_representative()
  348. self.get_person_info(representative)
  349. self.contact_person_function = representative.function
  350. else:
  351. self.get_person_info(partner)
  352. # declare this function in order to be overriden
  353. def get_eater_vals(self, partner, share_product_id): #noqa
  354. return {}
  355. def _prepare_invoice_line(self, product, partner, qty):
  356. self.ensure_one()
  357. account = product.property_account_income_id \
  358. or product.categ_id.property_account_income_categ_id
  359. if not account:
  360. raise UserError(_('Please define income account for this product:'
  361. ' "%s" (id:%d) - or for its category: "%s".') %
  362. (product.name, product.id, product.categ_id.name))
  363. fpos = partner.property_account_position_id
  364. if fpos:
  365. account = fpos.map_account(account)
  366. res = {
  367. 'name': product.name,
  368. 'account_id': account.id,
  369. 'price_unit': product.lst_price,
  370. 'quantity': qty,
  371. 'uom_id': product.uom_id.id,
  372. 'product_id': product.id or False,
  373. }
  374. return res
  375. def get_capital_release_mail_template(self):
  376. template = 'easy_my_coop.email_template_release_capital'
  377. return self.env.ref(template, False)
  378. def send_capital_release_request(self, invoice):
  379. email_template = self.get_capital_release_mail_template()
  380. # we send the email with the capital release request in attachment
  381. # TODO remove sudo() and give necessary access right
  382. email_template.sudo().send_mail(invoice.id, True)
  383. invoice.sent = True
  384. def get_journal(self):
  385. return self.env['account.journal'].search([('code', '=', 'SUBJ')])[0]
  386. def get_accounting_account(self):
  387. account_obj = self.env['account.account']
  388. if self.company_id.property_cooperator_account:
  389. account = self.company_id.property_cooperator_account
  390. else:
  391. accounts = account_obj.search([('code', '=', '416000')])
  392. if accounts:
  393. account = accounts[0]
  394. else:
  395. raise UserError(_(
  396. 'You must set a cooperator account on you company.'
  397. ))
  398. return account
  399. def get_invoice_vals(self, partner):
  400. return {
  401. 'partner_id': partner.id,
  402. 'journal_id': self.get_journal().id,
  403. 'account_id': self.get_accounting_account().id,
  404. 'type': 'out_invoice',
  405. 'release_capital_request': True,
  406. 'subscription_request': self.id
  407. }
  408. def create_invoice(self, partner):
  409. # creating invoice and invoice lines
  410. invoice_vals = self.get_invoice_vals(partner)
  411. if self.capital_release_request_date:
  412. invoice_vals['date_invoice'] = self.capital_release_request_date
  413. invoice = self.env['account.invoice'].create(invoice_vals)
  414. vals = self._prepare_invoice_line(self.share_product_id, partner,
  415. self.ordered_parts)
  416. vals['invoice_id'] = invoice.id
  417. self.env['account.invoice.line'].create(vals)
  418. # validate the capital release request
  419. invoice.action_invoice_open()
  420. self.send_capital_release_request(invoice)
  421. return invoice
  422. def get_partner_company_vals(self):
  423. partner_vals = {'name': self.company_name,
  424. 'last_name': self.company_name,
  425. 'is_company': self.is_company,
  426. 'company_register_number': self.company_register_number, # noqa
  427. 'customer': False, 'cooperator': True,
  428. 'street': self.address, 'zip': self.zip_code,
  429. 'city': self.city, 'email': self.company_email,
  430. 'out_inv_comm_type': 'bba',
  431. 'customer': self.share_product_id.customer,
  432. 'country_id': self.country_id.id,
  433. 'lang': self.lang,
  434. 'data_policy_approved': self.data_policy_approved,
  435. 'internal_rules_approved': self.internal_rules_approved
  436. }
  437. return partner_vals
  438. def get_partner_vals(self):
  439. partner_vals = {'name': self.name, 'firstname': self.firstname,
  440. 'lastname': self.lastname, 'street': self.address,
  441. 'zip': self.zip_code, 'email': self.email,
  442. 'gender': self.gender, 'cooperator': True,
  443. 'city': self.city, 'phone': self.phone,
  444. 'country_id': self.country_id.id, 'lang': self.lang,
  445. 'birthdate_date': self.birthdate,
  446. 'customer': self.share_product_id.customer,
  447. 'data_policy_approved': self.data_policy_approved,
  448. 'internal_rules_approved': self.internal_rules_approved
  449. }
  450. return partner_vals
  451. def get_representative_vals(self):
  452. contact_vals = {
  453. 'name': self.name,
  454. 'firstname': self.firstname,
  455. 'lastname': self.lastname, 'customer': False,
  456. 'is_company': False, 'cooperator': True,
  457. 'street': self.address, 'gender': self.gender,
  458. 'zip': self.zip_code, 'city': self.city,
  459. 'phone': self.phone, 'email': self.email,
  460. 'country_id': self.country_id.id,
  461. 'out_inv_comm_type': 'bba',
  462. 'out_inv_comm_algorithm': 'random',
  463. 'lang': self.lang,
  464. 'birthdate_date': self.birthdate,
  465. 'parent_id': self.partner_id.id,
  466. 'representative': True,
  467. 'function': self.contact_person_function,
  468. 'type': 'representative',
  469. 'data_policy_approved': self.data_policy_approved,
  470. 'internal_rules_approved': self.internal_rules_approved
  471. }
  472. return contact_vals
  473. def create_coop_partner(self):
  474. partner_obj = self.env['res.partner']
  475. if self.is_company:
  476. partner_vals = self.get_partner_company_vals()
  477. else:
  478. partner_vals = self.get_partner_vals()
  479. partner = partner_obj.create(partner_vals)
  480. if self.iban:
  481. self.env['res.partner.bank'].create({
  482. 'partner_id': partner.id,
  483. 'acc_number': self.iban
  484. })
  485. return partner
  486. def set_membership(self):
  487. # To be overridden
  488. return True
  489. @api.one # todo remove api.one
  490. def validate_subscription_request(self):
  491. # todo rename to validate (careful with iwp dependencies)
  492. partner_obj = self.env['res.partner']
  493. if self.ordered_parts <= 0:
  494. raise UserError(_('Number of share must be greater than 0.'))
  495. if self.partner_id:
  496. partner = self.partner_id
  497. else:
  498. partner = None
  499. domain = []
  500. if self.already_cooperator:
  501. raise UserError(_('The checkbox already cooperator is'
  502. ' checked please select a cooperator.'))
  503. elif self.is_company and self.company_register_number:
  504. domain = [('company_register_number', '=', self.company_register_number)] # noqa
  505. elif not self.is_company and self.email:
  506. domain = [('email', '=', self.email)]
  507. if domain:
  508. partner = partner_obj.search(domain)
  509. if not partner:
  510. partner = self.create_coop_partner()
  511. self.partner_id = partner
  512. else:
  513. partner = partner[0]
  514. partner.cooperator = True
  515. if self.is_company and not partner.has_representative():
  516. contact = False
  517. if self.email:
  518. domain = [('email', '=', self.email)]
  519. contact = partner_obj.search(domain)
  520. if contact:
  521. contact.type = 'representative'
  522. if not contact:
  523. contact_vals = self.get_representative_vals()
  524. partner_obj.create(contact_vals)
  525. else:
  526. if len(contact) > 1:
  527. raise UserError(_('There is two different persons with the'
  528. ' same national register number. Please'
  529. ' proceed to a merge before to continue')
  530. )
  531. if contact.parent_id and contact.parent_id.id != partner.id:
  532. raise UserError(_('This contact person is already defined'
  533. ' for another company. Please select'
  534. ' another contact'))
  535. else:
  536. contact.write({'parent_id': partner.id,
  537. 'representative': True})
  538. invoice = self.create_invoice(partner)
  539. self.write({'state': 'done'})
  540. self.set_membership()
  541. return invoice
  542. @api.one # todo remove api.one
  543. def block_subscription_request(self):
  544. self.write({'state': 'block'})
  545. @api.one # todo remove api.one
  546. def unblock_subscription_request(self):
  547. self.write({'state': 'draft'})
  548. @api.one # todo remove api.one
  549. def cancel_subscription_request(self):
  550. self.write({'state': 'cancelled'})
  551. @api.one # todo remove api.one
  552. def put_on_waiting_list(self):
  553. waiting_list_mail_template = self.env.ref('easy_my_coop.email_template_waiting_list', False)
  554. waiting_list_mail_template.send_mail(self.id, True)
  555. self.write({'state': 'waiting'})
  556. # todo move to share_line.py
  557. class ShareLine(models.Model):
  558. _name = 'share.line'
  559. _description = "Share line"
  560. @api.multi
  561. def _compute_total_line(self):
  562. res = {}
  563. for line in self:
  564. line.total_amount_line = line.share_unit_price * line.share_number
  565. return res
  566. share_product_id = fields.Many2one('product.product',
  567. string='Share type',
  568. required=True,
  569. readonly=True)
  570. share_number = fields.Integer(string='Number of Share',
  571. required=True,
  572. readonly=True)
  573. share_short_name = fields.Char(related='share_product_id.short_name',
  574. string='Share type name',
  575. readonly=True)
  576. share_unit_price = fields.Monetary(
  577. string='Share price',
  578. currency_field="company_currency_id",
  579. readonly=True,
  580. )
  581. effective_date = fields.Date(string='Effective Date',
  582. readonly=True)
  583. partner_id = fields.Many2one('res.partner',
  584. string='Cooperator',
  585. required=True,
  586. ondelete='cascade',
  587. readonly=True)
  588. total_amount_line = fields.Monetary(
  589. string='Total amount line',
  590. currency_field="company_currency_id",
  591. compute='_compute_total_line',
  592. )
  593. company_id = fields.Many2one(
  594. "res.company",
  595. string='Company',
  596. required=True,
  597. change_default=True, readonly=True,
  598. default=lambda self: self.env['res.company']._company_default_get(),
  599. )
  600. company_currency_id = fields.Many2one(
  601. "res.currency",
  602. string="Company Currency",
  603. related="company_id.currency_id",
  604. readonly=True,
  605. )
  606. # todo move to subscription_register.py
  607. class SubscriptionRegister(models.Model):
  608. _name = 'subscription.register'
  609. _description = "Subscription register"
  610. @api.multi
  611. def _compute_total_line(self):
  612. for line in self:
  613. line.total_amount_line = line.share_unit_price * line.quantity
  614. name = fields.Char(string='Number Operation',
  615. required=True,
  616. readonly=True)
  617. register_number_operation = fields.Integer(string='Register Number Operation',
  618. required=True,
  619. readonly=True)
  620. partner_id = fields.Many2one('res.partner',
  621. string='Cooperator',
  622. required=True,
  623. readonly=True)
  624. partner_id_to = fields.Many2one('res.partner',
  625. string='Transfered to',
  626. readonly=True)
  627. date = fields.Date(string='Subscription Date',
  628. required=True,
  629. readonly=True)
  630. quantity = fields.Integer(string='Number of share',
  631. readonly=True)
  632. share_unit_price = fields.Monetary(
  633. string='Share price',
  634. currency_field="company_currency_id",
  635. readonly=True,
  636. )
  637. total_amount_line = fields.Monetary(
  638. string='Total amount line',
  639. currency_field="company_currency_id",
  640. compute='_compute_total_line',
  641. )
  642. share_product_id = fields.Many2one('product.product',
  643. string='Share type',
  644. required=True,
  645. readonly=True,
  646. domain=[('is_share', '=', True)])
  647. share_short_name = fields.Char(related='share_product_id.short_name',
  648. string='Share type name',
  649. readonly=True)
  650. share_to_product_id = fields.Many2one('product.product',
  651. string='Share to type',
  652. readonly=True,
  653. domain=[('is_share', '=', True)])
  654. share_to_short_name = fields.Char(related='share_to_product_id.short_name',
  655. string='Share to type name',
  656. readonly=True)
  657. quantity_to = fields.Integer(string='Number of share to',
  658. readonly=True)
  659. share_to_unit_price = fields.Monetary(
  660. string='Share to price',
  661. currency_field="company_currency_id",
  662. readonly=True,
  663. )
  664. type = fields.Selection([('subscription', 'Subscription'),
  665. ('transfer', 'Transfer'),
  666. ('sell_back', 'Sell Back'),
  667. ('convert', 'Conversion')],
  668. string='Operation Type', readonly=True)
  669. company_id = fields.Many2one('res.company', string='Company',
  670. required=True,
  671. change_default=True, readonly=True,
  672. default=lambda self: self.env['res.company']._company_default_get())
  673. company_currency_id = fields.Many2one(
  674. "res.currency",
  675. related="company_id.currency_id",
  676. string="Company Currency",
  677. readonly=True,
  678. )
  679. user_id = fields.Many2one('res.users',
  680. string='Responsible',
  681. readonly=True,
  682. default=lambda self: self.env.user)
  683. _order = "register_number_operation asc"
  684. @api.model
  685. def read_group(self, domain, fields, groupby, offset=0, limit=None,
  686. orderby=False,
  687. lazy=True):
  688. if 'share_unit_price' in fields:
  689. fields.remove('share_unit_price')
  690. if 'register_number_operation' in fields:
  691. fields.remove('register_number_operation')
  692. res = super(SubscriptionRegister, self).read_group(domain, fields,
  693. groupby,
  694. offset=offset,
  695. limit=limit,
  696. orderby=orderby,
  697. lazy=lazy)
  698. if 'total_amount_line' in fields:
  699. for line in res:
  700. if '__domain' in line:
  701. lines = self.search(line['__domain'])
  702. inv_value = 0.0
  703. for line2 in lines:
  704. inv_value += line2.total_amount_line
  705. line['total_amount_line'] = inv_value
  706. return res