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.

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