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.

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