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.

236 lines
12 KiB

  1. # coding: utf-8
  2. # Copyright: Odoo S.A.
  3. # License: AGPL-3
  4. # flake8: noqa
  5. # pylint: skip-file
  6. from openerp.tools.translate import _
  7. from openerp import api
  8. @api.cr_uid_ids_context
  9. def _create_account_move_line(self, cr, uid, ids, session=None, move_id=None, context=None):
  10. """ Monkeypatch for this method's version on pos.order in the point_of_sale
  11. module. Only change is to refer to the line's taxes instead of the
  12. product's taxes (change below is marked with 'pos_pricelist'). Keep in a
  13. separate file so that it can be excluded from flake8 inspection. """
  14. if True: # Keep indentation level for reference purposes
  15. # Tricky, via the workflow, we only have one id in the ids variable
  16. """Create a account move line of order grouped by products or not."""
  17. account_move_obj = self.pool.get('account.move')
  18. account_period_obj = self.pool.get('account.period')
  19. account_tax_obj = self.pool.get('account.tax')
  20. property_obj = self.pool.get('ir.property')
  21. cur_obj = self.pool.get('res.currency')
  22. #session_ids = set(order.session_id for order in self.browse(cr, uid, ids, context=context))
  23. if session and not all(session.id == order.session_id.id for order in self.browse(cr, uid, ids, context=context)):
  24. raise osv.except_osv(_('Error!'), _('Selected orders do not have the same session!'))
  25. grouped_data = {}
  26. have_to_group_by = session and session.config_id.group_by or False
  27. def compute_tax(amount, tax, line):
  28. if amount > 0:
  29. tax_code_id = tax['base_code_id']
  30. tax_amount = line.price_subtotal * tax['base_sign']
  31. else:
  32. tax_code_id = tax['ref_base_code_id']
  33. tax_amount = abs(line.price_subtotal) * tax['ref_base_sign']
  34. return (tax_code_id, tax_amount,)
  35. for order in self.browse(cr, uid, ids, context=context):
  36. if order.account_move:
  37. continue
  38. if order.state != 'paid':
  39. continue
  40. current_company = order.sale_journal.company_id
  41. group_tax = {}
  42. account_def = property_obj.get(cr, uid, 'property_account_receivable', 'res.partner', context=context)
  43. order_account = order.partner_id and \
  44. order.partner_id.property_account_receivable and \
  45. order.partner_id.property_account_receivable.id or \
  46. account_def and account_def.id
  47. if move_id is None:
  48. # Create an entry for the sale
  49. move_id = self._create_account_move(cr, uid, order.session_id.start_at, order.name, order.sale_journal.id, order.company_id.id, context=context)
  50. move = account_move_obj.browse(cr, uid, move_id, context=context)
  51. def insert_data(data_type, values):
  52. # if have_to_group_by:
  53. sale_journal_id = order.sale_journal.id
  54. # 'quantity': line.qty,
  55. # 'product_id': line.product_id.id,
  56. values.update({
  57. 'date': order.date_order[:10],
  58. 'ref': order.name,
  59. 'partner_id': order.partner_id and self.pool.get("res.partner")._find_accounting_partner(order.partner_id).id or False,
  60. 'journal_id' : sale_journal_id,
  61. 'period_id': move.period_id.id,
  62. 'move_id' : move_id,
  63. 'company_id': current_company.id,
  64. })
  65. if data_type == 'product':
  66. key = ('product', values['partner_id'], values['product_id'], values['analytic_account_id'], values['debit'] > 0)
  67. elif data_type == 'tax':
  68. key = ('tax', values['partner_id'], values['tax_code_id'], values['debit'] > 0)
  69. elif data_type == 'counter_part':
  70. key = ('counter_part', values['partner_id'], values['account_id'], values['debit'] > 0)
  71. else:
  72. return
  73. grouped_data.setdefault(key, [])
  74. # if not have_to_group_by or (not grouped_data[key]):
  75. # grouped_data[key].append(values)
  76. # else:
  77. # pass
  78. if have_to_group_by:
  79. if not grouped_data[key]:
  80. grouped_data[key].append(values)
  81. else:
  82. for line in grouped_data[key]:
  83. if line.get('tax_code_id') == values.get('tax_code_id'):
  84. current_value = line
  85. current_value['quantity'] = current_value.get('quantity', 0.0) + values.get('quantity', 0.0)
  86. current_value['credit'] = current_value.get('credit', 0.0) + values.get('credit', 0.0)
  87. current_value['debit'] = current_value.get('debit', 0.0) + values.get('debit', 0.0)
  88. current_value['tax_amount'] = current_value.get('tax_amount', 0.0) + values.get('tax_amount', 0.0)
  89. break
  90. else:
  91. grouped_data[key].append(values)
  92. else:
  93. grouped_data[key].append(values)
  94. #because of the weird way the pos order is written, we need to make sure there is at least one line,
  95. #because just after the 'for' loop there are references to 'line' and 'income_account' variables (that
  96. #are set inside the for loop)
  97. #TOFIX: a deep refactoring of this method (and class!) is needed in order to get rid of this stupid hack
  98. assert order.lines, _('The POS order must have lines when calling this method')
  99. # Create an move for each order line
  100. cur = order.pricelist_id.currency_id
  101. round_per_line = True
  102. if order.company_id.tax_calculation_rounding_method == 'round_globally':
  103. round_per_line = False
  104. for line in order.lines:
  105. tax_amount = 0
  106. taxes = []
  107. # [pos_pricelist] Only change in the next line:
  108. # for t in line.product_id.taxes_id:
  109. for t in line.tax_ids if 'tax_ids' in line._fields else line.product_id.taxes_id:
  110. if t.company_id.id == current_company.id:
  111. taxes.append(t)
  112. computed_taxes = account_tax_obj.compute_all(cr, uid, taxes, line.price_unit * (100.0-line.discount) / 100.0, line.qty)['taxes']
  113. for tax in computed_taxes:
  114. tax_amount += cur_obj.round(cr, uid, cur, tax['amount']) if round_per_line else tax['amount']
  115. if tax_amount < 0:
  116. group_key = (tax['ref_tax_code_id'], tax['base_code_id'], tax['account_collected_id'], tax['id'])
  117. else:
  118. group_key = (tax['tax_code_id'], tax['base_code_id'], tax['account_collected_id'], tax['id'])
  119. group_tax.setdefault(group_key, 0)
  120. group_tax[group_key] += cur_obj.round(cr, uid, cur, tax['amount']) if round_per_line else tax['amount']
  121. amount = line.price_subtotal
  122. # Search for the income account
  123. if line.product_id.property_account_income.id:
  124. income_account = line.product_id.property_account_income.id
  125. elif line.product_id.categ_id.property_account_income_categ.id:
  126. income_account = line.product_id.categ_id.property_account_income_categ.id
  127. else:
  128. raise osv.except_osv(_('Error!'), _('Please define income '\
  129. 'account for this product: "%s" (id:%d).') \
  130. % (line.product_id.name, line.product_id.id, ))
  131. # Empty the tax list as long as there is no tax code:
  132. tax_code_id = False
  133. tax_amount = 0
  134. while computed_taxes:
  135. tax = computed_taxes.pop(0)
  136. tax_code_id, tax_amount = compute_tax(amount, tax, line)
  137. # If there is one we stop
  138. if tax_code_id:
  139. break
  140. # Create a move for the line
  141. insert_data('product', {
  142. 'name': line.product_id.name,
  143. 'quantity': line.qty,
  144. 'product_id': line.product_id.id,
  145. 'account_id': income_account,
  146. 'analytic_account_id': self._prepare_analytic_account(cr, uid, line, context=context),
  147. 'credit': ((amount>0) and amount) or 0.0,
  148. 'debit': ((amount<0) and -amount) or 0.0,
  149. 'tax_code_id': tax_code_id,
  150. 'tax_amount': tax_amount,
  151. 'partner_id': order.partner_id and self.pool.get("res.partner")._find_accounting_partner(order.partner_id).id or False
  152. })
  153. # For each remaining tax with a code, whe create a move line
  154. for tax in computed_taxes:
  155. tax_code_id, tax_amount = compute_tax(amount, tax, line)
  156. if not tax_code_id:
  157. continue
  158. insert_data('tax', {
  159. 'name': _('Tax'),
  160. 'product_id':line.product_id.id,
  161. 'quantity': line.qty,
  162. 'account_id': income_account,
  163. 'credit': 0.0,
  164. 'debit': 0.0,
  165. 'tax_code_id': tax_code_id,
  166. 'tax_amount': tax_amount,
  167. 'partner_id': order.partner_id and self.pool.get("res.partner")._find_accounting_partner(order.partner_id).id or False
  168. })
  169. # Create a move for each tax group
  170. (tax_code_pos, base_code_pos, account_pos, tax_id)= (0, 1, 2, 3)
  171. for key, tax_amount in group_tax.items():
  172. tax = self.pool.get('account.tax').browse(cr, uid, key[tax_id], context=context)
  173. insert_data('tax', {
  174. 'name': _('Tax') + ' ' + tax.name,
  175. 'quantity': line.qty,
  176. 'product_id': line.product_id.id,
  177. 'account_id': key[account_pos] or income_account,
  178. 'credit': ((tax_amount>0) and tax_amount) or 0.0,
  179. 'debit': ((tax_amount<0) and -tax_amount) or 0.0,
  180. 'tax_code_id': key[tax_code_pos],
  181. 'tax_amount': abs(tax_amount) * tax.tax_sign if tax_amount>=0 else abs(tax_amount) * tax.ref_tax_sign,
  182. 'partner_id': order.partner_id and self.pool.get("res.partner")._find_accounting_partner(order.partner_id).id or False
  183. })
  184. # counterpart
  185. insert_data('counter_part', {
  186. 'name': _("Trade Receivables"), #order.name,
  187. 'account_id': order_account,
  188. 'credit': ((order.amount_total < 0) and -order.amount_total) or 0.0,
  189. 'debit': ((order.amount_total > 0) and order.amount_total) or 0.0,
  190. 'partner_id': order.partner_id and self.pool.get("res.partner")._find_accounting_partner(order.partner_id).id or False
  191. })
  192. order.write({'state':'done', 'account_move': move_id})
  193. all_lines = []
  194. for group_key, group_data in grouped_data.iteritems():
  195. for value in group_data:
  196. all_lines.append((0, 0, value),)
  197. if move_id: #In case no order was changed
  198. self.pool.get("account.move").write(cr, uid, [move_id], {'line_id':all_lines}, context=context)
  199. return True