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.

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