Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to perform all checks on lines in Odoo?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 586
    Comment on it

    If you want to perform all checks on lines in Odoo follow the following code:

      if 'account_id' not in vals or 'journal_id' not in vals:
                raise UserError(_("It is mandatory to specify an account and a journal to create a write-off."))
            if ('debit' in vals) ^ ('credit' in vals):
                raise UserError(_("Either pass both debit and credit or none."))
            if 'date' not in vals:
                vals['date'] = self._context.get('date_p') or time.strftime('%Y-%m-%d')
            if 'name' not in vals:
                vals['name'] = self._context.get('comment') or _('Write-Off')
            #compute the writeoff amount if not given
            if 'credit' not in vals and 'debit' not in vals:
                amount = sum([r.amount_residual for r in self])
                vals['credit'] = amount > 0 and amount or 0.0
                vals['debit'] = amount < 0 and abs(amount) or 0.0
            vals['partner_id'] = self.env['res.partner']._find_accounting_partner(self[0].partner_id).id
            company_currency = self[0].account_id.company_id.currency_id
            account_currency = self[0].account_id.currency_id or company_currency
            if 'amount_currency' not in vals and account_currency != company_currency:
                vals['currency_id'] = account_currency
                vals['amount_currency'] = sum([r.amount_residual_currency for r in self])
    
            # Writeoff line in the account of self
            first_line_dict = vals.copy()
            first_line_dict['account_id'] = self[0].account_id.id
            if 'analytic_account_id' in vals:
                del vals['analytic_account_id']
    
            # Writeoff line in specified writeoff account
            second_line_dict = vals.copy()
            second_line_dict['debit'], second_line_dict['credit'] = second_line_dict['credit'], second_line_dict['debit']
            if 'amount_currency' in vals:
                second_line_dict['amount_currency'] = -second_line_dict['amount_currency']
    
            # Create the move
            writeoff_move = self.env['account.move'].create({
                'journal_id': vals['journal_id'],
                'date': vals['date'],
                'state': 'draft',
                'line_ids': [(0, 0, first_line_dict), (0, 0, second_line_dict)],
            })
            writeoff_move.post()
    
            # Return the writeoff move.line which is to be reconciled
            return writeoff_move.line_ids.filtered(lambda r: r.account_id == self[0].account_id)
    
        @api.multi
        def remove_move_reconcile(self):
            """ Undo a reconciliation """
            if not self:
                return True
            rec_move_ids = self.env['account.partial.reconcile']
            for account_move_line in self:
                rec_move_ids += account_move_line.matched_debit_ids
                rec_move_ids += account_move_line.matched_credit_ids
            return rec_move_ids.unlink()
     company_ids = set()
            all_accounts = []
            partners = set()
            for line in self:
                company_ids.add(line.company_id.id)
                all_accounts.append(line.account_id)
                if (line.account_id.internal_type in ('receivable', 'payable')):
                    partners.add(line.partner_id.id)
                if line.reconciled:
                    raise UserError(_('You are trying to reconcile some entries that are already reconciled!'))
            if len(company_ids) > 1:
                raise UserError(_('To reconcile the entries company should be the same for all entries!'))
            if len(set(all_accounts)) > 1:
                raise UserError(_('Entries are not of the same account!'))
            if not all_accounts[0].reconcile:
                raise UserError(_('The account %s (%s) is not marked as reconciliable !') % (all_accounts[0].name, all_accounts[0].code))
            if len(partners) > 1:
                raise UserError(_('The partner has to be the same on all lines for receivable and payable accounts!'))
    
            #reconcile everything that can be
            remaining_moves = self.auto_reconcile_lines()
    
            #if writeoff_acc_id specified, then create write-off move with value the remaining amount from move in self
            if writeoff_acc_id and writeoff_journal_id and remaining_moves:
                writeoff_to_reconcile = remaining_moves._create_writeoff({'account_id': writeoff_acc_id.id, 'journal_id': writeoff_journal_id.id})
                #add writeoff line to reconcile algo and finish the reconciliation
                remaining_moves = (remaining_moves + writeoff_to_reconcile).auto_reconcile_lines()
    
        def _create_writeoff(self, vals):
            """ Create a writeoff move for the account.move.lines in self. If debit/credit is not specified in vals,
                the writeoff amount will be computed as the sum of amount_residual of the given recordset.
    
                :param vals: dict containing values suitable fot account_move_line.create(). The data in vals will
                    be processed to create bot writeoff acount.move.line and their enclosing account.move.
            """
            # Check and complete vals
            if 'account_id' not in vals or 'journal_id' not in vals:
                raise UserError(_("It is mandatory to specify an account and a journal to create a write-off."))
            if ('debit' in vals) ^ ('credit' in vals):
                raise UserError(_("Either pass both debit and credit or none."))
            if 'date' not in vals:
                vals['date'] = self._context.get('date_p') or time.strftime('%Y-%m-%d')
            if 'name' not in vals:
                vals['name'] = self._context.get('comment') or _('Write-Off')
            #compute the writeoff amount if not given
            if 'credit' not in vals and 'debit' not in vals:
                amount = sum([r.amount_residual for r in self])
                vals['credit'] = amount > 0 and amount or 0.0
                vals['debit'] = amount < 0 and abs(amount) or 0.0
            vals['partner_id'] = self.env['res.partner']._find_accounting_partner(self[0].partner_id).id
            company_currency = self[0].account_id.company_id.currency_id
            account_currency = self[0].account_id.currency_id or company_currency
            if 'amount_currency' not in vals and account_currency != company_currency:
                vals['currency_id'] = account_currency
                vals['amount_currency'] = sum([r.amount_residual_currency for r in self])
    
            # Writeoff line in the account of self
            first_line_dict = vals.copy()
            first_line_dict['account_id'] = self[0].account_id.id
            if 'analytic_account_id' in vals:
                del vals['analytic_account_id']
    
            # Writeoff line in specified writeoff account
            second_line_dict = vals.copy()
            second_line_dict['debit'], second_line_dict['credit'] = second_line_dict['credit'], second_line_dict['debit']
            if 'amount_currency' in vals:
                second_line_dict['amount_currency'] = -second_line_dict['amount_currency']
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: