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.

767 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. if not vals.get('partner_id'):
  50. cooperator = False
  51. if vals.get('email'):
  52. cooperator = partner_obj.get_cooperator_from_email(
  53. vals.get('email'))
  54. if cooperator:
  55. # TODO remove the following line once it has
  56. # been found a way to avoid double encoding
  57. cooperator = cooperator[0]
  58. vals['type'] = 'subscription'
  59. vals = self.is_member(vals, cooperator)
  60. vals['partner_id'] = cooperator.id
  61. else:
  62. cooperator_id = vals.get('partner_id')
  63. cooperator = partner_obj.browse(cooperator_id)
  64. vals = self.is_member(vals, cooperator)
  65. if not cooperator.cooperator:
  66. cooperator.write({'cooperator': True})
  67. subscr_request = super(SubscriptionRequest, self).create(vals)
  68. mail_template_notif = subscr_request.get_mail_template_notif(False)
  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. vals["name"] = vals['company_name']
  74. if not vals.get('partner_id'):
  75. cooperator = self.env['res.partner'].get_cooperator_from_crn(vals.get('company_register_number'))
  76. if cooperator:
  77. vals['partner_id'] = cooperator.id
  78. vals['type'] = 'increase'
  79. vals['already_cooperator'] = True
  80. subscr_request = super(SubscriptionRequest, self).create(vals)
  81. confirmation_mail_template = subscr_request.get_mail_template_notif(True)
  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.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:
  506. partner = self.create_coop_partner()
  507. self.partner_id = partner
  508. else:
  509. partner = partner[0]
  510. partner.cooperator = True
  511. if self.is_company and not partner.has_representative():
  512. contact = False
  513. if self.email:
  514. domain = [('email', '=', self.email)]
  515. contact = partner_obj.search(domain)
  516. if contact:
  517. contact.type = 'representative'
  518. if not contact:
  519. contact_vals = self.get_representative_vals()
  520. partner_obj.create(contact_vals)
  521. else:
  522. if len(contact) > 1:
  523. raise UserError(_('There is two different persons with the'
  524. ' same national register number. Please'
  525. ' proceed to a merge before to continue')
  526. )
  527. if contact.parent_id and contact.parent_id.id != partner.id:
  528. raise UserError(_('This contact person is already defined'
  529. ' for another company. Please select'
  530. ' another contact'))
  531. else:
  532. contact.write({'parent_id': partner.id,
  533. 'representative': True})
  534. invoice = self.create_invoice(partner)
  535. self.write({'state': 'done'})
  536. self.set_membership()
  537. return invoice
  538. @api.one
  539. def block_subscription_request(self):
  540. self.write({'state': 'block'})
  541. @api.one
  542. def unblock_subscription_request(self):
  543. self.write({'state': 'draft'})
  544. @api.one
  545. def cancel_subscription_request(self):
  546. self.write({'state': 'cancelled'})
  547. @api.one
  548. def put_on_waiting_list(self):
  549. self.write({'state': 'waiting'})
  550. class ShareLine(models.Model):
  551. _name = 'share.line'
  552. _description = "Share line"
  553. @api.multi
  554. def _compute_total_line(self):
  555. res = {}
  556. for line in self:
  557. line.total_amount_line = line.share_unit_price * line.share_number
  558. return res
  559. share_product_id = fields.Many2one('product.product',
  560. string='Share type',
  561. required=True,
  562. readonly=True)
  563. share_number = fields.Integer(string='Number of Share',
  564. required=True,
  565. readonly=True)
  566. share_short_name = fields.Char(related='share_product_id.short_name',
  567. string='Share type name',
  568. readonly=True)
  569. share_unit_price = fields.Monetary(
  570. string='Share price',
  571. currency_field="company_currency_id",
  572. readonly=True,
  573. )
  574. effective_date = fields.Date(string='Effective Date',
  575. readonly=True)
  576. partner_id = fields.Many2one('res.partner',
  577. string='Cooperator',
  578. required=True,
  579. ondelete='cascade',
  580. readonly=True)
  581. total_amount_line = fields.Monetary(
  582. string='Total amount line',
  583. currency_field="company_currency_id",
  584. compute='_compute_total_line',
  585. )
  586. company_id = fields.Many2one(
  587. "res.company",
  588. string='Company',
  589. required=True,
  590. change_default=True, readonly=True,
  591. default=lambda self: self.env['res.company']._company_default_get(),
  592. )
  593. company_currency_id = fields.Many2one(
  594. "res.currency",
  595. string="Company Currency",
  596. related="company_id.currency_id",
  597. readonly=True,
  598. )
  599. class SubscriptionRegister(models.Model):
  600. _name = 'subscription.register'
  601. _description = "Subscription register"
  602. @api.multi
  603. def _compute_total_line(self):
  604. for line in self:
  605. line.total_amount_line = line.share_unit_price * line.quantity
  606. name = fields.Char(string='Number Operation',
  607. required=True,
  608. readonly=True)
  609. register_number_operation = fields.Integer(string='Register Number Operation',
  610. required=True,
  611. readonly=True)
  612. partner_id = fields.Many2one('res.partner',
  613. string='Cooperator',
  614. required=True,
  615. readonly=True)
  616. partner_id_to = fields.Many2one('res.partner',
  617. string='Transfered to',
  618. readonly=True)
  619. date = fields.Date(string='Subscription Date',
  620. required=True,
  621. readonly=True)
  622. quantity = fields.Integer(string='Number of share',
  623. readonly=True)
  624. share_unit_price = fields.Monetary(
  625. string='Share price',
  626. currency_field="company_currency_id",
  627. readonly=True,
  628. )
  629. total_amount_line = fields.Monetary(
  630. string='Total amount line',
  631. currency_field="company_currency_id",
  632. compute='_compute_total_line',
  633. )
  634. share_product_id = fields.Many2one('product.product',
  635. string='Share type',
  636. required=True,
  637. readonly=True,
  638. domain=[('is_share', '=', True)])
  639. share_short_name = fields.Char(related='share_product_id.short_name',
  640. string='Share type name',
  641. readonly=True)
  642. share_to_product_id = fields.Many2one('product.product',
  643. string='Share to type',
  644. readonly=True,
  645. domain=[('is_share', '=', True)])
  646. share_to_short_name = fields.Char(related='share_to_product_id.short_name',
  647. string='Share to type name',
  648. readonly=True)
  649. quantity_to = fields.Integer(string='Number of share to',
  650. readonly=True)
  651. share_to_unit_price = fields.Monetary(
  652. string='Share to price',
  653. currency_field="company_currency_id",
  654. readonly=True,
  655. )
  656. type = fields.Selection([('subscription', 'Subscription'),
  657. ('transfer', 'Transfer'),
  658. ('sell_back', 'Sell Back'),
  659. ('convert', 'Conversion')],
  660. string='Operation Type', readonly=True)
  661. company_id = fields.Many2one('res.company', string='Company',
  662. required=True,
  663. change_default=True, readonly=True,
  664. default=lambda self: self.env['res.company']._company_default_get())
  665. company_currency_id = fields.Many2one(
  666. "res.currency",
  667. related="company_id.currency_id",
  668. string="Company Currency",
  669. readonly=True,
  670. )
  671. user_id = fields.Many2one('res.users',
  672. string='Responsible',
  673. readonly=True,
  674. default=lambda self: self.env.user)
  675. _order = "register_number_operation asc"
  676. @api.model
  677. def read_group(self, domain, fields, groupby, offset=0, limit=None,
  678. orderby=False,
  679. lazy=True):
  680. if 'share_unit_price' in fields:
  681. fields.remove('share_unit_price')
  682. if 'register_number_operation' in fields:
  683. fields.remove('register_number_operation')
  684. res = super(SubscriptionRegister, self).read_group(domain, fields,
  685. groupby,
  686. offset=offset,
  687. limit=limit,
  688. orderby=orderby,
  689. lazy=lazy)
  690. if 'total_amount_line' in fields:
  691. for line in res:
  692. if '__domain' in line:
  693. lines = self.search(line['__domain'])
  694. inv_value = 0.0
  695. for line2 in lines:
  696. inv_value += line2.total_amount_line
  697. line['total_amount_line'] = inv_value
  698. return res