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.

686 lines
32 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. from openerp import api, fields, models, _
  4. from openerp.addons.base_iban import base_iban
  5. from openerp.exceptions import UserError, ValidationError
  6. _REQUIRED = ['email',
  7. 'firstname',
  8. 'lastname',
  9. 'birthdate',
  10. 'address',
  11. 'share_product_id',
  12. 'ordered_parts',
  13. 'zip_code',
  14. 'city',
  15. 'iban',
  16. 'no_registre',
  17. 'gender'] # Could be improved including required from model
  18. @api.model
  19. def _lang_get(self):
  20. languages = self.env['res.lang'].search([])
  21. return [(language.code, language.name) for language in languages]
  22. class subscription_request(models.Model):
  23. _name = 'subscription.request'
  24. _description = 'Subscription Request'
  25. def get_required_field(self):
  26. return _REQUIRED
  27. @api.model
  28. def create(self, vals):
  29. partner_obj = self.env['res.partner']
  30. if not vals.get('partner_id'):
  31. cooperator = False
  32. if vals.get('no_registre'):
  33. cooperator = partner_obj.get_cooperator_from_nin(
  34. vals.get('no_registre'))
  35. if cooperator:
  36. # TODO remove the following line of code once it has
  37. # been founded a way to avoid dubble entry
  38. cooperator = cooperator[0]
  39. if cooperator.member:
  40. vals['type'] = 'increase'
  41. vals['already_cooperator'] = True
  42. else:
  43. vals['type'] = 'subscription'
  44. vals['partner_id'] = cooperator.id
  45. if not cooperator.cooperator:
  46. cooperator.write({'cooperator': True})
  47. else:
  48. cooperator_id = vals.get('partner_id')
  49. cooperator = partner_obj.browse(cooperator_id)
  50. if cooperator.member:
  51. vals['type'] = 'increase'
  52. vals['already_cooperator'] = True
  53. subscr_request = super(subscription_request, self).create(vals)
  54. confirmation_mail_template = self.env.ref('easy_my_coop.email_template_confirmation', False)
  55. confirmation_mail_template.send_mail(subscr_request.id)
  56. return subscr_request
  57. @api.model
  58. def create_comp_sub_req(self, vals):
  59. if not vals.get('partner_id'):
  60. cooperator = self.env['res.partner'].get_cooperator_from_crn(vals.get('company_register_number'))
  61. if cooperator:
  62. vals['partner_id'] = cooperator.id
  63. vals['type'] = 'increase'
  64. vals['already_cooperator'] = True
  65. subscr_request = super(subscription_request, self).create(vals)
  66. confirmation_mail_template = self.env.ref('easy_my_coop.email_template_confirmation_company', False)
  67. confirmation_mail_template.send_mail(subscr_request.id, True)
  68. return subscr_request
  69. def check_belgian_identification_id(self, nat_register_num):
  70. if not self.check_empty_string(nat_register_num):
  71. return False
  72. if len(nat_register_num) != 11:
  73. return False
  74. if not nat_register_num.isdigit():
  75. return False
  76. birthday_number = nat_register_num[0:9]
  77. controle = nat_register_num[9:11]
  78. check_controle = 97 - (int(birthday_number) % 97)
  79. if int(check_controle) != int(controle):
  80. check_controle = 97 - ((2000000000 + int(birthday_number)) % 97)
  81. if int(check_controle) != int(controle):
  82. return False
  83. return True
  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. try:
  91. base_iban.validate_iban(iban)
  92. except ValidationError:
  93. validated = False
  94. return validated
  95. @api.multi
  96. @api.depends('iban', 'no_registre', 'skip_control_ng', 'is_company')
  97. def _validated_lines(self):
  98. for sub_request in self:
  99. validated = self.check_iban(sub_request.iban)
  100. if validated and (sub_request.skip_control_ng or
  101. self.check_belgian_identification_id(
  102. sub_request.no_registre)):
  103. validated = True
  104. else:
  105. validated = False
  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.Float(compute='_compute_subscription_amount',
  179. string='Subscription amount',
  180. readonly=True,
  181. states={'draft': [('readonly', False)]})
  182. ordered_parts = fields.Integer(string='Number of Share',
  183. required=True,
  184. readonly=True,
  185. states={'draft': [('readonly', False)]})
  186. address = fields.Char(string='Address',
  187. required=True,
  188. readonly=True,
  189. states={'draft': [('readonly', False)]})
  190. city = fields.Char(string='City',
  191. required=True,
  192. readonly=True,
  193. states={'draft': [('readonly', False)]})
  194. zip_code = fields.Char(string='Zip Code',
  195. required=True,
  196. readonly=True,
  197. states={'draft': [('readonly', False)]})
  198. country_id = fields.Many2one('res.country',
  199. string='Country',
  200. ondelete='restrict',
  201. required=True,
  202. readonly=True,
  203. states={'draft': [('readonly', False)]})
  204. phone = fields.Char(string='Phone',
  205. readonly=True,
  206. states={'draft': [('readonly', False)]})
  207. no_registre = fields.Char(string='National Register Number',
  208. readonly=True,
  209. states={'draft': [('readonly', False)]})
  210. user_id = fields.Many2one('res.users',
  211. string='Responsible',
  212. readonly=True)
  213. validated = fields.Boolean(compute='_validated_lines',
  214. string='Valid Line?',
  215. readonly=True)
  216. skip_control_ng = fields.Boolean(string="Skip control",
  217. help="if this field is checked then no"
  218. " control will be done on the national"
  219. " register number and on the iban bank"
  220. " account. To be done in case of the id"
  221. " card is from abroad or in case of"
  222. " a passport",
  223. readonly=True,
  224. states={'draft': [('readonly', False)]})
  225. lang = fields.Selection(_lang_get,
  226. string='Language',
  227. required=True,
  228. readonly=True,
  229. states={'draft': [('readonly', False)]},
  230. default=lambda self: self.env['res.company']._company_default_get().default_lang_id.code)
  231. date = fields.Date(string='Subscription date request',
  232. required=True,
  233. readonly=True,
  234. states={'draft': [('readonly', False)]},
  235. default=lambda self: datetime.strftime(datetime.now(), '%Y-%m-%d'))
  236. company_id = fields.Many2one('res.company', string='Company', required=True,
  237. change_default=True,
  238. readonly=True,
  239. default=lambda self: self.env['res.company']._company_default_get())
  240. is_company = fields.Boolean(string='Is a company',
  241. readonly=True,
  242. states={'draft': [('readonly', False)]})
  243. is_operation = fields.Boolean(string='Is an operation',
  244. readonly=True,
  245. states={'draft': [('readonly', False)]})
  246. company_name = fields.Char(string="Company name",
  247. readonly=True,
  248. states={'draft': [('readonly', False)]})
  249. company_email = fields.Char(string="Company email",
  250. readonly=True,
  251. states={'draft': [('readonly', False)]})
  252. company_register_number = fields.Char(string='Company register number',
  253. readonly=True,
  254. states={'draft': [('readonly', False)]})
  255. company_type = fields.Selection([('scrl', 'SCRL'),
  256. ('asbl', 'ASBL'),
  257. ('sprl', 'SPRL'),
  258. ('sa', 'SA'),
  259. ('other', 'Other')],
  260. string="Company type",
  261. readonly=True,
  262. states={'draft': [('readonly', False)]})
  263. same_address = fields.Boolean(string='Same address',
  264. readonly=True,
  265. states={'draft': [('readonly', False)]})
  266. activities_address = fields.Char(string='Activities address',
  267. readonly=True,
  268. states={'draft': [('readonly', False)]})
  269. activities_city = fields.Char(string='Activities city',
  270. readonly=True,
  271. states={'draft': [('readonly', False)]})
  272. activities_zip_code = fields.Char(string='Activities zip Code',
  273. readonly=True,
  274. states={'draft': [('readonly', False)]})
  275. activities_country_id = fields.Many2one('res.country',
  276. string='Activities country',
  277. ondelete='restrict',
  278. readonly=True,
  279. states={'draft': [('readonly', False)]})
  280. contact_person_function = fields.Char(string='Function',
  281. readonly=True,
  282. states={'draft': [('readonly', False)]})
  283. operation_request_id = fields.Many2one('operation.request',
  284. string="Operation Request",
  285. readonly=True,
  286. states={'draft': [('readonly', False)]})
  287. capital_release_request = fields.One2many('account.invoice',
  288. 'subscription_request',
  289. string='Capital release request',
  290. readonly=True,
  291. states={'draft': [('readonly', False)]})
  292. capital_release_request_date = fields.Date(string="Force the capital "
  293. "release request date",
  294. help="Keep empty to use the "
  295. "current date",
  296. copy=False,
  297. readonly=True,
  298. states={'draft': [('readonly', False)]})
  299. source = fields.Selection([('website', 'Website'),
  300. ('crm', 'CRM'),
  301. ('manual', 'Manual'),
  302. ('operation', 'Operation')],
  303. string="Source",
  304. default="website",
  305. readonly=True,
  306. states={'draft': [('readonly', False)]})
  307. _order = "id desc"
  308. def get_person_info(self, partner):
  309. self.firstname = partner.first_name
  310. self.name = partner.name
  311. self.lastname = partner.last_name
  312. self.no_registre = partner.national_register_number
  313. self.email = partner.email
  314. self.birthdate = partner.birthdate_date
  315. self.gender = partner.gender
  316. self.address = partner.street
  317. self.city = partner.city
  318. self.zip_code = partner.zip
  319. self.country_id = partner.country_id
  320. self.phone = partner.phone
  321. self.lang = partner.lang
  322. @api.onchange('partner_id')
  323. def onchange_partner(self):
  324. partner = self.partner_id
  325. if partner:
  326. self.is_company = partner.is_company
  327. self.already_cooperator = partner.member
  328. if partner.bank_ids:
  329. self.iban = partner.bank_ids[0].acc_number
  330. if partner.member:
  331. self.type = 'increase'
  332. if partner.is_company:
  333. self.company_name = partner.name
  334. self.company_email = partner.email
  335. self.company_register_number = partner.company_register_number
  336. representative = partner.get_representative()
  337. self.get_person_info(representative)
  338. self.contact_person_function = representative.function
  339. else:
  340. self.get_person_info(partner)
  341. # declare this function in order to be overriden
  342. def get_eater_vals(self, partner, share_product_id): #noqa
  343. return {}
  344. def _prepare_invoice_line(self, product, partner, qty):
  345. self.ensure_one()
  346. account = product.property_account_income_id \
  347. or product.categ_id.property_account_income_categ_id
  348. if not account:
  349. raise UserError(_('Please define income account for this product:'
  350. ' "%s" (id:%d) - or for its category: "%s".') %
  351. (product.name, product.id, product.categ_id.name))
  352. fpos = partner.property_account_position_id
  353. if fpos:
  354. account = fpos.map_account(account)
  355. res = {
  356. 'name': product.name,
  357. 'account_id': account.id,
  358. 'price_unit': product.lst_price,
  359. 'quantity': qty,
  360. 'uom_id': product.uom_id.id,
  361. 'product_id': product.id or False,
  362. }
  363. return res
  364. def send_capital_release_request(self, invoice):
  365. invoice_email_template = self.env['mail.template'].search([('name', '=', 'Request to Release Capital - Send by Email')])[0]
  366. # we send the email with the capital release request in attachment
  367. invoice_email_template.send_mail(invoice.id, True)
  368. invoice.sent = True
  369. def create_invoice(self, partner):
  370. # get subscription journal
  371. journal = self.env['account.journal'].search([('code', '=', 'SUBJ')])[0]
  372. # get the account for associate
  373. # TODO this should be defined in configuration
  374. if self.company_id.property_cooperator_account:
  375. account = self.company_id.property_cooperator_account
  376. else:
  377. account = self.env['account.account'].search([('code', '=', '416000')])[0]
  378. # creating invoice and invoice lines
  379. invoice_vals = {'partner_id': partner.id,
  380. 'journal_id': journal.id,
  381. 'account_id': account.id,
  382. 'type': 'out_invoice',
  383. 'release_capital_request': True,
  384. 'subscription_request': self.id}
  385. if self.capital_release_request_date:
  386. invoice_vals['date_invoice'] = self.capital_release_request_date
  387. invoice = self.env['account.invoice'].create(invoice_vals)
  388. vals = self._prepare_invoice_line(self.share_product_id, partner,
  389. self.ordered_parts)
  390. vals['invoice_id'] = invoice.id
  391. self.env['account.invoice.line'].create(vals)
  392. # validate the capital release request
  393. invoice.signal_workflow('invoice_open')
  394. self.send_capital_release_request(invoice)
  395. return invoice
  396. def get_partner_company_vals(self):
  397. partner_vals = {'name': self.company_name,
  398. 'last_name': self.company_name,
  399. 'is_company': self.is_company,
  400. 'company_register_number': self.company_register_number, #noqa
  401. 'customer': False, 'cooperator': True,
  402. 'street': self.address, 'zip': self.zip_code,
  403. 'city': self.city, 'email': self.company_email,
  404. 'out_inv_comm_type': 'bba',
  405. 'customer': self.share_product_id.customer,
  406. 'out_inv_comm_algorithm': 'random',
  407. 'country_id': self.country_id.id,
  408. 'lang': self.lang}
  409. return partner_vals
  410. def get_partner_vals(self):
  411. partner_vals = {'name': self.name, 'first_name': self.firstname,
  412. 'last_name': self.lastname, 'street': self.address,
  413. 'zip': self.zip_code, 'email': self.email,
  414. 'gender': self.gender, 'cooperator': True,
  415. 'city': self.city, 'phone': self.phone,
  416. 'national_register_number': self.no_registre,
  417. 'out_inv_comm_type': 'bba',
  418. 'out_inv_comm_algorithm': 'random',
  419. 'country_id': self.country_id.id, 'lang': self.lang,
  420. 'birthdate_date': self.birthdate,
  421. 'customer': self.share_product_id.customer}
  422. return partner_vals
  423. def create_coop_partner(self):
  424. partner_obj = self.env['res.partner']
  425. if self.is_company:
  426. partner_vals = self.get_partner_company_vals()
  427. else:
  428. partner_vals = self.get_partner_vals()
  429. partner = partner_obj.create(partner_vals)
  430. if self.iban:
  431. self.env['res.partner.bank'].create({
  432. 'partner_id': partner.id,
  433. 'acc_number': self.iban
  434. })
  435. return partner
  436. @api.one
  437. def validate_subscription_request(self):
  438. partner_obj = self.env['res.partner']
  439. if self.ordered_parts <= 0:
  440. raise UserError(_('Number of share must be greater than 0.'))
  441. if self.partner_id:
  442. if not self.partner_id.cooperator:
  443. self.partner_id.cooperator = True
  444. partner = self.partner_id
  445. else:
  446. partner = None
  447. if self.already_cooperator:
  448. raise UserError(_('The checkbox already cooperator is'
  449. ' checked please select a cooperator.'))
  450. elif self.is_company and self.company_register_number:
  451. domain = [('company_register_number', '=', self.company_register_number)] #noqa
  452. elif not self.is_company and self.no_registre:
  453. domain = [('national_register_number', '=', self.no_registre)]
  454. partner = partner_obj.search(domain)
  455. if not partner:
  456. partner = self.create_coop_partner()
  457. else:
  458. partner = partner[0]
  459. if self.is_company and not partner.has_representative():
  460. contact = False
  461. if self.no_registre:
  462. domain = [('national_register_number', '=', self.no_registre)]
  463. contact = partner_obj.search(domain)
  464. if contact:
  465. contact.type = 'representative'
  466. if not contact:
  467. contact_vals = {'name': self.name,
  468. 'first_name': self.firstname,
  469. 'last_name': self.lastname, 'customer': False,
  470. 'is_company': False, 'cooperator': True,
  471. 'street': self.address, 'gender': self.gender,
  472. 'zip': self.zip_code, 'city': self.city,
  473. 'phone': self.phone, 'email': self.email,
  474. 'national_register_number': self.no_registre,
  475. 'country_id': self.country_id.id,
  476. 'out_inv_comm_type': 'bba',
  477. 'out_inv_comm_algorithm': 'random',
  478. 'lang': self.lang,
  479. 'birthdate_date': self.birthdate,
  480. 'parent_id': partner.id,
  481. 'representative': True,
  482. 'function': self.contact_person_function,
  483. 'type': 'representative'}
  484. contact = partner_obj.create(contact_vals)
  485. else:
  486. if len(contact) > 1:
  487. raise UserError(_('There is two different persons with the'
  488. ' same national register number. Please'
  489. ' proceed to a merge before to continue')
  490. )
  491. if contact.parent_id and contact.parent_id.id != partner.id:
  492. raise UserError(_('This contact person is already defined'
  493. ' for another company. Please select'
  494. ' another contact'))
  495. else:
  496. contact.write({'parent_id': partner.id,
  497. 'representative': True})
  498. invoice = self.create_invoice(partner)
  499. self.write({'partner_id': partner.id, 'state': 'done'})
  500. return invoice
  501. @api.one
  502. def block_subscription_request(self):
  503. self.write({'state': 'block'})
  504. @api.one
  505. def unblock_subscription_request(self):
  506. self.write({'state': 'draft'})
  507. @api.one
  508. def cancel_subscription_request(self):
  509. self.write({'state': 'cancelled'})
  510. @api.one
  511. def put_on_waiting_list(self):
  512. self.write({'state': 'waiting'})
  513. class share_line(models.Model):
  514. _name = 'share.line'
  515. @api.multi
  516. def _compute_total_line(self):
  517. res = {}
  518. for line in self:
  519. line.total_amount_line = line.share_unit_price * line.share_number
  520. return res
  521. share_product_id = fields.Many2one('product.product',
  522. string='Share type',
  523. required=True,
  524. readonly=True)
  525. share_number = fields.Integer(string='Number of Share',
  526. required=True,
  527. readonly=True)
  528. share_short_name = fields.Char(related='share_product_id.short_name',
  529. string='Share type name',
  530. readonly=True)
  531. share_unit_price = fields.Float(string='Share price',
  532. readonly=True)
  533. effective_date = fields.Date(string='Effective Date',
  534. readonly=True)
  535. partner_id = fields.Many2one('res.partner',
  536. string='Cooperator',
  537. required=True,
  538. ondelete='cascade',
  539. readonly=True)
  540. total_amount_line = fields.Float(compute='_compute_total_line',
  541. string='Total amount line')
  542. class subscription_register(models.Model):
  543. _name = 'subscription.register'
  544. @api.multi
  545. def _compute_total_line(self):
  546. for register_line in self:
  547. register_line.total_amount_line = register_line.share_unit_price * register_line.quantity
  548. name = fields.Char(string='Register Number Operation',
  549. required=True,
  550. readonly=True)
  551. register_number_operation = fields.Integer(string='Register Number Operation',
  552. required=True,
  553. readonly=True)
  554. partner_id = fields.Many2one('res.partner',
  555. string='Cooperator',
  556. required=True,
  557. readonly=True)
  558. partner_id_to = fields.Many2one('res.partner',
  559. string='Transfered to',
  560. readonly=True)
  561. date = fields.Date(string='Subscription Date',
  562. required=True,
  563. readonly=True)
  564. quantity = fields.Integer(string='Number of share',
  565. readonly=True)
  566. share_unit_price = fields.Float(string='Share price',
  567. readonly=True)
  568. total_amount_line = fields.Float(compute='_compute_total_line',
  569. string='Total amount line')
  570. share_product_id = fields.Many2one('product.product',
  571. string='Share type',
  572. required=True,
  573. readonly=True,
  574. domain=[('is_share', '=', True)])
  575. share_short_name = fields.Char(related='share_product_id.short_name',
  576. string='Share type name',
  577. readonly=True)
  578. share_to_product_id = fields.Many2one('product.product',
  579. string='Share to type',
  580. readonly=True,
  581. domain=[('is_share', '=', True)])
  582. share_to_short_name = fields.Char(related='share_to_product_id.short_name',
  583. string='Share to type name',
  584. readonly=True)
  585. quantity_to = fields.Integer(string='Number of share to',
  586. readonly=True)
  587. share_to_unit_price = fields.Float(string='Share to price',
  588. readonly=True)
  589. type = fields.Selection([('subscription', 'Subscription'),
  590. ('transfer', 'Transfer'),
  591. ('sell_back', 'Sell Back'),
  592. ('convert', 'Conversion')],
  593. string='Operation Type', readonly=True)
  594. company_id = fields.Many2one('res.company', string='Company',
  595. required=True,
  596. change_default=True, readonly=True,
  597. default=lambda self: self.env['res.company']._company_default_get())
  598. user_id = fields.Many2one('res.users',
  599. string='Responsible',
  600. readonly=True,
  601. default=lambda self: self.env.user)
  602. _order = "register_number_operation asc"
  603. @api.model
  604. def read_group(self, domain, fields, groupby, offset=0, limit=None,
  605. orderby=False,
  606. lazy=True):
  607. if 'share_unit_price' in fields:
  608. fields.remove('share_unit_price')
  609. if 'register_number_operation' in fields:
  610. fields.remove('register_number_operation')
  611. res = super(subscription_register, self).read_group(domain, fields,
  612. groupby,
  613. offset=offset,
  614. limit=limit,
  615. orderby=orderby,
  616. lazy=lazy)
  617. if 'total_amount_line' in fields:
  618. for line in res:
  619. if '__domain' in line:
  620. lines = self.search(line['__domain'])
  621. inv_value = 0.0
  622. for line2 in lines:
  623. inv_value += line2.total_amount_line
  624. line['total_amount_line'] = inv_value
  625. return res