'When new quant arrive in a location, try to reconcile it with
negative quants. If it\'s possible, apply the cost of the new
quant to the counterpart of the negative quant.'

@api.one
def _quant_reconcile_negative(self, move):

    solving_quant = self
    quants = self._search_quants_to_reconcile()
    product_uom_rounding = self.product_id.uom_id.rounding
    for (quant_neg, qty) in quants:
        if ((not quant_neg) or (not solving_quant)):
            continue
        quants_to_solve = self.search([('propagated_from_id', '=', quant_neg.id)])
        if (not quants_to_solve):
            continue
        solving_qty = qty
        solved_quants = self.env['stock.quant'].sudo()
        for to_solve_quant in quants_to_solve:
            if (float_compare(solving_qty, 0, precision_rounding=product_uom_rounding) <= 0):
                continue
            solved_quants |= to_solve_quant
            to_solve_quant._quant_split(min(solving_qty, to_solve_quant.qty))
            solving_qty -= min(solving_qty, to_solve_quant.qty)
        remaining_solving_quant = solving_quant._quant_split(qty)
        remaining_neg_quant = quant_neg._quant_split((- qty))
        if remaining_neg_quant:
            remaining_to_solves = self.sudo().search([('propagated_from_id', '=', quant_neg.id), ('id', 'not    in', solved_quants.ids)])
            if remaining_to_solves:
                remaining_to_solves.write({'propagated_from_id': remaining_neg_quant.id})
        if (solving_quant.propagated_from_id and solved_quants):
            solved_quants.write({'propagated_from_id': solving_quant.propagated_from_id.id})
        quant_neg.sudo().with_context(force_unlink=True).unlink()
        if solved_quants:
            solved_quants._price_update(solving_quant.cost)
            solved_quants.write(solving_quant._prepare_history())
        solving_quant.with_context(force_unlink=True).unlink()
        solving_quant = remaining_solving_quant
