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.

300 lines
12 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Vincent Renaville, ported by Joel Grand-Guillaume
  5. # Copyright 2010-2012 Camptocamp SA
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import orm, fields
  22. class AccountHoursBlock(orm.Model):
  23. _name = "account.hours.block"
  24. def _get_last_action(self, cr, uid, ids, name, arg, context=None):
  25. """ Return the last analytic line date for an invoice"""
  26. res = {}
  27. for block in self.browse(cr, uid, ids, context=context):
  28. cr.execute("SELECT max(al.date) FROM account_analytic_line AS al"
  29. " WHERE al.invoice_id = %s", (block.invoice_id.id,))
  30. fetch_res = cr.fetchone()
  31. res[block.id] = fetch_res[0] if fetch_res else False
  32. return res
  33. def _compute_hours(self, cr, uid, ids, fields, args, context=None):
  34. """Return a dict of [id][fields]"""
  35. if isinstance(ids, (int, long)):
  36. ids = [ids]
  37. result = {}
  38. aal_obj = self.pool.get('account.analytic.line')
  39. for block in self.browse(cr, uid, ids, context=context):
  40. result[block.id] = {'amount_hours_block': 0.0,
  41. 'amount_hours_block_done': 0.0}
  42. # Compute hours bought
  43. for line in block.invoice_id.invoice_line:
  44. hours_bought = 0.0
  45. if line.product_id:
  46. # We will now calculate the product_quantity
  47. factor = line.uos_id.factor
  48. if factor == 0.0:
  49. factor = 1.0
  50. amount = line.quantity
  51. hours_bought += (amount / factor)
  52. result[block.id]['amount_hours_block'] += hours_bought
  53. # Compute hours spent
  54. hours_used = 0.0
  55. # Get ids of analytic line generated from
  56. # timesheet associated to the current block
  57. cr.execute("SELECT al.id "
  58. "FROM account_analytic_line AS al, "
  59. " account_analytic_journal AS aj "
  60. "WHERE aj.id = al.journal_id "
  61. "AND aj.type = 'general' "
  62. "AND al.invoice_id = %s", (block.invoice_id.id,))
  63. res_line_ids = cr.fetchall()
  64. line_ids = [l[0] for l in res_line_ids] if res_line_ids else []
  65. for line in aal_obj.browse(cr, uid, line_ids, context=context):
  66. factor = 1.0
  67. if line.product_uom_id and line.product_uom_id.factor != 0.0:
  68. factor = line.product_uom_id.factor
  69. factor_invoicing = 1.0
  70. if line.to_invoice and line.to_invoice.factor != 0.0:
  71. factor_invoicing = 1.0 - line.to_invoice.factor / 100
  72. hours_used += ((line.unit_amount / factor) * factor_invoicing)
  73. result[block.id]['amount_hours_block_done'] = hours_used
  74. return result
  75. def _compute_amount(self, cr, uid, ids, fields, args, context=None):
  76. if context is None:
  77. context = {}
  78. result = {}
  79. aal_obj = self.pool.get('account.analytic.line')
  80. pricelist_obj = self.pool.get('product.pricelist')
  81. for block in self.browse(cr, uid, ids, context=context):
  82. result[block.id] = {'amount_hours_block': 0.0,
  83. 'amount_hours_block_done': 0.0}
  84. # Compute amount bought
  85. for line in block.invoice_id.invoice_line:
  86. amount_bought = 0.0
  87. if line.product_id:
  88. ## We will now calculate the product_quantity
  89. factor = line.uos_id.factor
  90. if factor == 0.0:
  91. factor = 1.0
  92. amount = line.quantity * line.price_unit
  93. amount_bought += (amount / factor)
  94. result[block.id]['amount_hours_block'] += amount_bought
  95. # Compute total amount
  96. # Get ids of analytic line generated from timesheet associated to current block
  97. cr.execute("SELECT al.id FROM account_analytic_line AS al,"
  98. " account_analytic_journal AS aj"
  99. " WHERE aj.id = al.journal_id"
  100. " AND aj.type='general'"
  101. " AND al.invoice_id = %s", (block.invoice_id.id,))
  102. res_line_ids = cr.fetchall()
  103. line_ids = [l[0] for l in res_line_ids] if res_line_ids else []
  104. total_amount = 0.0
  105. for line in aal_obj.browse(cr, uid, line_ids, context=context):
  106. factor_invoicing = 1.0
  107. if line.to_invoice and line.to_invoice.factor != 0.0:
  108. factor_invoicing = 1.0 - line.to_invoice.factor / 100
  109. ctx = dict(context, uom=line.product_uom_id.id)
  110. amount = pricelist_obj.price_get(
  111. cr, uid,
  112. [line.account_id.pricelist_id.id],
  113. line.product_id.id,
  114. line.unit_amount or 1.0,
  115. line.account_id.partner_id.id or False,
  116. ctx)[line.account_id.pricelist_id.id]
  117. total_amount += amount * line.unit_amount * factor_invoicing
  118. result[block.id]['amount_hours_block_done'] += total_amount
  119. return result
  120. def _compute(self, cr, uid, ids, fields, args, context=None):
  121. result = {}
  122. block_per_types = {}
  123. for block in self.browse(cr, uid, ids, context=context):
  124. block_per_types.setdefault(block.type, []).append(block.id)
  125. for block_type in block_per_types:
  126. if block_type:
  127. func = getattr(self, "_compute_%s" % block_type)
  128. result.update(func(cr, uid, ids, fields, args, context=context))
  129. for block in result:
  130. result[block]['amount_hours_block_delta'] = \
  131. result[block]['amount_hours_block'] - \
  132. result[block]['amount_hours_block_done']
  133. return result
  134. _columns = {
  135. 'amount_hours_block': fields.function(
  136. _compute,
  137. type='float',
  138. string='Quantity / Amount bought',
  139. store=True,
  140. multi='amount_hours_block_delta',
  141. help="Amount bought by the customer. "
  142. "This amount is expressed in the base Unit of Measure "
  143. "(factor=1.0)"),
  144. 'amount_hours_block_done': fields.function(
  145. _compute,
  146. type='float',
  147. string='Quantity / Amount used',
  148. store=True,
  149. multi='amount_hours_block_delta',
  150. help="Amount done by the staff. "
  151. "This amount is expressed in the base Unit of Measure "
  152. "(factor=1.0)"),
  153. 'amount_hours_block_delta': fields.function(
  154. _compute,
  155. type='float',
  156. string='Difference',
  157. store=True,
  158. multi='amount_hours_block_delta',
  159. help="Difference between bought and used. "
  160. "This amount is expressed in the base Unit of Measure "
  161. "(factor=1.0)"),
  162. 'last_action_date': fields.function(
  163. _get_last_action,
  164. type='date',
  165. string='Last action date',
  166. help="Date of the last analytic line linked to the invoice "
  167. "related to this block hours."),
  168. 'close_date': fields.date('Closed Date'),
  169. 'invoice_id': fields.many2one(
  170. 'account.invoice',
  171. 'Invoice',
  172. ondelete='cascade',
  173. required=True),
  174. 'type': fields.selection(
  175. [('hours', 'Hours'),
  176. ('amount', 'Amount')],
  177. string='Type of Block',
  178. required=True,
  179. help="The block is based on the quantity of hours "
  180. "or on the amount."),
  181. # Invoices related infos
  182. 'date_invoice': fields.related(
  183. 'invoice_id', 'date_invoice',
  184. type="date",
  185. string="Invoice Date",
  186. store=True,
  187. readonly=True),
  188. 'user_id': fields.related(
  189. 'invoice_id', 'user_id',
  190. type="many2one",
  191. relation="res.users",
  192. string="Salesman",
  193. store=True,
  194. readonly=True),
  195. 'partner_id': fields.related(
  196. 'invoice_id', 'partner_id',
  197. type="many2one",
  198. relation="res.partner",
  199. string="Partner",
  200. store=True,
  201. readonly=True),
  202. 'name': fields.related(
  203. 'invoice_id', 'name',
  204. type="char",
  205. size=64,
  206. string="Description",
  207. store=True,
  208. readonly=True),
  209. 'number': fields.related(
  210. 'invoice_id', 'number',
  211. type="char",
  212. size=64,
  213. string="Number",
  214. store=True,
  215. readonly=True),
  216. 'journal_id': fields.related(
  217. 'invoice_id', 'journal_id',
  218. type="many2one",
  219. relation="account.journal",
  220. string="Journal",
  221. store=True,
  222. readonly=True),
  223. 'period_id': fields.related(
  224. 'invoice_id', 'period_id',
  225. type="many2one",
  226. relation="account.period",
  227. string="Period",
  228. store=True,
  229. readonly=True),
  230. 'company_id': fields.related(
  231. 'invoice_id', 'company_id',
  232. type="many2one",
  233. relation="res.company",
  234. string="Company",
  235. store=True,
  236. readonly=True),
  237. 'currency_id': fields.related(
  238. 'invoice_id', 'currency_id',
  239. type="many2one",
  240. relation="res.currency",
  241. string="Currency",
  242. store=True,
  243. readonly=True),
  244. 'residual': fields.related(
  245. 'invoice_id', 'residual',
  246. type="float",
  247. string="Residual",
  248. store=True,
  249. readonly=True),
  250. 'amount_total': fields.related(
  251. 'invoice_id', 'amount_total',
  252. type="float",
  253. string="Total",
  254. store=True,
  255. readonly=True),
  256. 'state': fields.related(
  257. 'invoice_id', 'state',
  258. type='selection',
  259. selection=[
  260. ('draft', 'Draft'),
  261. ('proforma', 'Pro-forma'),
  262. ('proforma2', 'Pro-forma'),
  263. ('open', 'Open'),
  264. ('paid', 'Paid'),
  265. ('cancel', 'Cancelled'),
  266. ],
  267. string='State',
  268. readonly=True,
  269. store=True),
  270. }
  271. ############################################################################
  272. ## Add hours blocks on invoice
  273. ############################################################################
  274. class AccountInvoice(orm.Model):
  275. _inherit = 'account.invoice'
  276. _columns = {
  277. 'account_hours_block_ids': fields.one2many(
  278. 'account.hours.block',
  279. 'invoice_id',
  280. string='Hours Block')
  281. }