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.

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