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.

344 lines
18 KiB

  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. import openerp.addons.decimal_precision as dp
  7. _REQUIRED = ['email','firstname','lastname','birthdate','address','share_product_id','ordered_parts','zip_code','city','iban','no_registre','gender'] # Could be improved including required from model
  8. @api.model
  9. def _lang_get(self):
  10. languages = self.env['res.lang'].search([])
  11. return [(language.code, language.name) for language in languages]
  12. class subscription_request(models.Model):
  13. _name = 'subscription.request'
  14. _description = 'Subscription Request'
  15. def get_required_field(self):
  16. return _REQUIRED
  17. @api.model
  18. def create(self,vals):
  19. if not vals.get('partner_id'):
  20. cooperator = False
  21. if vals.get('no_registre'):
  22. cooperator = self.env['res.partner'].get_cooperator_from_nin(vals.get('no_registre'))
  23. if cooperator:
  24. if cooperator.member:
  25. vals['type'] = 'increase'
  26. vals['already_cooperator'] = True
  27. else:
  28. vals['type'] = 'subscription'
  29. vals['partner_id'] = cooperator.id
  30. subscr_request = super(subscription_request, self).create(vals)
  31. mail_template_obj = self.env['mail.template']
  32. confirmation_mail_template = mail_template_obj.search([('name', '=', 'Confirmation Email')])[0]
  33. confirmation_mail_template.send_mail(subscr_request.id, True)
  34. return subscr_request
  35. @api.model
  36. def create_comp_sub_req(self, vals):
  37. if not vals.get('partner_id'):
  38. cooperator = self.env['res.partner'].get_cooperator_from_crn(vals.get('company_register_number'))
  39. if cooperator:
  40. vals['partner_id'] = cooperator.id
  41. vals['type'] = 'increase'
  42. vals['already_cooperator'] = True
  43. subscr_request = super(subscription_request, self).create(vals)
  44. mail_template_obj = self.env['mail.template']
  45. confirmation_mail_template = mail_template_obj.search([('name', '=', 'Company Confirmation Email')])[0]
  46. confirmation_mail_template.send_mail(subscr_request.id, True)
  47. return subscr_request
  48. def check_belgian_identification_id(self, nat_register_num):
  49. if not self.check_empty_string(nat_register_num):
  50. return False
  51. if len(nat_register_num) != 11:
  52. return False
  53. if not nat_register_num.isdigit():
  54. return False
  55. birthday_number = nat_register_num[0:9]
  56. controle = nat_register_num[9:11]
  57. check_controle = 97 - (int(birthday_number) % 97)
  58. if int(check_controle) != int(controle):
  59. check_controle = 97 - ((2000000000 + int(birthday_number)) % 97)
  60. if int(check_controle) != int(controle):
  61. return False
  62. return True
  63. def check_empty_string(self, value):
  64. if value == None or value == False or value == '':
  65. return False
  66. return True
  67. @api.multi
  68. @api.depends('iban', 'no_registre','skip_control_ng')
  69. def _validated_lines(self):
  70. for sub_request in self:
  71. try:
  72. base_iban.validate_iban(sub_request.iban)
  73. sub_request.validated = True
  74. except ValidationError:
  75. sub_request.validated = False
  76. if not sub_request.is_company and (sub_request.skip_control_ng or self.check_belgian_identification_id(sub_request.no_registre)):
  77. sub_request.validated = True
  78. @api.multi
  79. @api.depends('share_product_id', 'share_product_id.list_price','ordered_parts')
  80. def _compute_subscription_amount(self):
  81. for sub_request in self:
  82. sub_request.subscription_amount = sub_request.share_product_id.list_price * sub_request.ordered_parts
  83. already_cooperator = fields.Boolean(string="I'm already cooperator")
  84. name = fields.Char(string='Name', required=True)
  85. firstname = fields.Char(string='Firstname')
  86. lastname = fields.Char(string='Lastname')
  87. birthdate = fields.Date(string="Birthdate")
  88. gender = fields.Selection([('male', 'Male'),
  89. ('female', 'Female'),
  90. ('other', 'Other')], string='Gender')
  91. type = fields.Selection([('new','New Cooperator'),
  92. ('subscription','Subscription'),
  93. ('increase','Increase number of share')]
  94. ,string='Type', default="new")
  95. state = fields.Selection([('draft','Draft'),
  96. ('block','Blocked'),
  97. ('done','Done'),
  98. ('cancelled','Cancelled'),
  99. ('paid','paid')],
  100. string='State',required=True, default="draft")
  101. email = fields.Char(string='Email')
  102. iban = fields.Char(string='Account Number')
  103. partner_id = fields.Many2one('res.partner',string='Cooperator')
  104. share_product_id = fields.Many2one('product.product', string='Share type', domain=[('is_share','=',True)])
  105. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name')
  106. share_unit_price = fields.Float(related='share_product_id.list_price', string='Share price')
  107. subscription_amount = fields.Float(compute='_compute_subscription_amount', string='Subscription amount')
  108. ordered_parts = fields.Integer(string='Number of Share')
  109. address = fields.Char(string='Address')
  110. city = fields.Char(string='City')
  111. zip_code = fields.Char(string='Zip Code')
  112. country_id = fields.Many2one('res.country', string='Country', ondelete='restrict')
  113. phone = fields.Char(string='Phone')
  114. no_registre = fields.Char(string='National Register Number')
  115. user_id = fields.Many2one('res.users', string='Responsible', readonly=True)
  116. validated = fields.Boolean(compute='_validated_lines', string='Valid Line?', readonly=True)
  117. skip_control_ng = fields.Boolean(string="Skip control",
  118. help="if this field is checked then no control will be done on the national register number and on the iban bank account. To be done in case of the id card is from abroad or in case of a passport")
  119. lang = fields.Selection(_lang_get, 'Language', default='fr_BE',
  120. help="If the selected language is loaded in the system, all documents related to this contact will be printed in this language. If not, it will be English.")
  121. date = fields.Date(string='Subscription date request', default=lambda self: datetime.strftime(datetime.now(), '%Y-%m-%d'))
  122. company_id = fields.Many2one('res.company', string='Company', required=True,
  123. change_default=True, readonly=True,
  124. default=lambda self: self.env['res.company']._company_default_get())
  125. is_company = fields.Boolean(string='Is a company')
  126. is_operation = fields.Boolean(string='Is an operation')
  127. company_name = fields.Char(string="Company name")
  128. company_email = fields.Char(string="Company email")
  129. company_register_number = fields.Char(string='Company register number')
  130. company_type = fields.Selection([('scrl','SCRL'),
  131. ('asbl','ASBL'),
  132. ('sprl','SPRL'),
  133. ('sa','SA'),
  134. ('other','Other')])
  135. same_address = fields.Boolean(string='Same address')
  136. activities_address = fields.Char(string='Activities address')
  137. activities_city = fields.Char(string='Activities city')
  138. activities_zip_code = fields.Char(string='Activities zip Code')
  139. activities_country_id = fields.Many2one('res.country', string='Activities country', ondelete='restrict')
  140. contact_person_function = fields.Char(string='Function')
  141. operation_request_id = fields.Many2one('operation.request', string="Operation Request")
  142. is_operation = fields.Boolean(string="Is Operation request")
  143. capital_release_request = fields.One2many('account.invoice','subscription_request', string='Subscription request')
  144. _order = "id desc"
  145. def _prepare_invoice_line(self, product, partner, qty):
  146. self.ensure_one()
  147. res = {}
  148. account = product.property_account_income_id or product.categ_id.property_account_income_categ_id
  149. if not account:
  150. raise UserError(_('Please define income account for this product: "%s" (id:%d) - or for its category: "%s".') % \
  151. (product.name, product.id, product.categ_id.name))
  152. fpos = partner.property_account_position_id
  153. if fpos:
  154. account = fpos.map_account(account)
  155. res = {
  156. 'name': product.name,
  157. 'account_id': account.id,
  158. 'price_unit': product.lst_price,
  159. 'quantity': qty,
  160. 'uom_id': product.uom_id.id,
  161. 'product_id': product.id or False,
  162. }
  163. return res
  164. def create_invoice(self, partner):
  165. # get subscription journal
  166. journal = self.env['account.journal'].search([('code','=','SUBJ')])[0]
  167. # get the account for associate
  168. # TODO this should be defined in configuration
  169. if self.company_id.property_cooperator_account:
  170. account = self.company_id.property_cooperator_account
  171. else:
  172. account = self.env['account.account'].search([('code','=','416000')])[0]
  173. # creating invoice and invoice lines
  174. invoice = self.env['account.invoice'].create({'partner_id':partner.id,
  175. 'journal_id':journal.id,'account_id':account.id,
  176. 'type': 'out_invoice', 'release_capital_request':True,
  177. 'subscription_request':self.id})
  178. vals = self._prepare_invoice_line(self.share_product_id, partner, self.ordered_parts)
  179. vals['invoice_id'] = invoice.id
  180. line = self.env['account.invoice.line'].create(vals)
  181. # validate the capital release request
  182. invoice.signal_workflow('invoice_open')
  183. invoice_email_template = self.env['mail.template'].search([('name', '=', 'Request to Release Capital - Send by Email')])[0]
  184. # we send the email with the capital release request in attachment
  185. invoice_email_template.send_mail(invoice.id, True)
  186. invoice.sent = True
  187. return invoice
  188. def get_partner_company_vals(self):
  189. # this should go to the many2many tag field
  190. #'title':'company',
  191. #self.env['res.partner.title'].search([('shortcut','=',self.company_type)])
  192. partner_vals = {'name':self.company_name, 'is_company': self.is_company,
  193. 'company_register_number':self.company_register_number, 'customer':False,
  194. 'cooperator':True, 'street':self.address, 'zip':self.zip_code,
  195. 'city': self.city,'email':self.email, 'out_inv_comm_type':'bba',
  196. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id, 'lang':self.lang}
  197. return partner_vals
  198. def get_partner_vals(self):
  199. partner_vals = {'name':self.name, 'first_name':self.firstname, 'last_name': self.lastname,
  200. 'customer':False, 'gender':self.gender,'cooperator':True, 'street':self.address,'zip':self.zip_code,
  201. 'city': self.city, 'phone': self.phone, 'email':self.email,
  202. 'national_register_number':self.no_registre, 'out_inv_comm_type':'bba',
  203. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id,
  204. 'lang':self.lang, 'birthdate':self.birthdate}
  205. return partner_vals
  206. def create_coop_partner(self):
  207. partner_obj = self.env['res.partner']
  208. if self.is_company:
  209. partner_vals = self.get_partner_company_vals()
  210. else:
  211. partner_vals = self.get_partner_vals()
  212. partner = partner_obj.create(partner_vals)
  213. if self.iban :
  214. self.env['res.partner.bank'].create({'partner_id':partner.id,'acc_number':self.iban})
  215. return partner
  216. @api.one
  217. def validate_subscription_request(self):
  218. partner_obj = self.env['res.partner']
  219. if self.partner_id:
  220. partner = self.partner_id
  221. else:
  222. if self.already_cooperator:
  223. raise UserError(_('The checkbox already cooperator is checked please select a cooperator.'))
  224. elif self.is_company:
  225. partner = partner_obj.search([('company_register_number','=',self.company_register_number)])
  226. elif self.no_registre:
  227. partner = partner_obj.search([('national_register_number','=',self.no_registre)])
  228. else:
  229. partner = None
  230. if not partner:
  231. partner = self.create_coop_partner()
  232. if self.is_company:
  233. contact = partner_obj.search([('national_register_number','=',self.no_registre)])
  234. if not contact:
  235. contact_vals = {'name':self.name, 'first_name':self.firstname, 'last_name': self.lastname,
  236. 'customer':False, 'street':self.address,'zip':self.zip_code,
  237. 'city': self.city, 'phone': self.phone, 'email':self.email,
  238. 'national_register_number':self.no_registre, 'out_inv_comm_type':'bba',
  239. 'out_inv_comm_algorithm':'random', 'country_id': self.country_id.id,
  240. 'lang':self.lang, 'birthdate':self.birthdate, 'parent_id': partner.id,'function':self.contact_person_function}
  241. contact = partner_obj.create(contact_vals)
  242. else:
  243. partner = partner[0]
  244. invoice = self.create_invoice(partner)
  245. self.write({'partner_id':partner.id, 'state':'done'})
  246. return invoice
  247. @api.one
  248. def block_subscription_request(self):
  249. self.write({'state':'block'})
  250. @api.one
  251. def unblock_subscription_request(self):
  252. self.write({'state':'draft'})
  253. @api.one
  254. def cancel_subscription_request(self):
  255. self.write({'state':'cancelled'})
  256. class share_line(models.Model):
  257. _name='share.line'
  258. @api.multi
  259. def _compute_total_line(self):
  260. res = {}
  261. for line in self:
  262. line.total_amount_line = line.share_unit_price * line.share_number
  263. return res
  264. share_product_id = fields.Many2one('product.product', string='Share type', required=True, readonly=True)
  265. share_number = fields.Integer(string='Number of Share', required=True, readonly=True)
  266. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name')
  267. share_unit_price = fields.Float(string='Share price', readonly=True)
  268. effective_date = fields.Date(string='Effective Date', readonly=True)
  269. partner_id = fields.Many2one('res.partner',string='Cooperator', required=True, ondelete='cascade', readonly=True)
  270. total_amount_line = fields.Float(compute='_compute_total_line', string='Total amount line')
  271. class subscription_register(models.Model):
  272. _name= 'subscription.register'
  273. @api.multi
  274. def _compute_total_line(self):
  275. res = {}
  276. for register_line in self:
  277. register_line.total_amount_line = register_line.share_unit_price * register_line.quantity
  278. name = fields.Char(string='Register Number Operation', required=True, readonly=True)
  279. register_number_operation = fields.Integer(string='Register Number Operation', required=True, readonly=True)
  280. partner_id = fields.Many2one('res.partner',string='Cooperator', required=True, readonly=True)
  281. partner_id_to = fields.Many2one('res.partner',string='Transfered to', readonly=True)
  282. date = fields.Date(string='Subscription Date', required= True, readonly=True)
  283. quantity = fields.Integer(string='Number of share', readonly=True)
  284. share_unit_price = fields.Float(string='Share price', readonly=True)
  285. total_amount_line = fields.Float(compute='_compute_total_line', string='Total amount line')
  286. share_product_id = fields.Many2one('product.product', string='Share type', required=True, readonly=True, domain=[('is_share','=',True)])
  287. share_short_name = fields.Char(related='share_product_id.short_name', string='Share type name', readonly=True)
  288. type = fields.Selection([('subscription','Subscription'),
  289. ('transfer','Transfer'),
  290. ('sell_back','Sell Back'),
  291. ('convert','Conversion')],
  292. string='Operation Type', readonly=True)
  293. company_id = fields.Many2one('res.company', string='Company', required=True,
  294. change_default=True, readonly=True,
  295. default=lambda self: self.env['res.company']._company_default_get())
  296. user_id = fields.Many2one('res.users', string='Responsible', readonly=True, default=lambda self: self.env.user)
  297. _order = "register_number_operation asc"