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.

233 lines
12 KiB

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