0
votes

I've been testing and searching for a solution to my problem but I'm not able to find any, thanks in advance to anyone that helps.

In Odoo 11 I'm trying to build invoices for suppliers from stock moves so I've added a tree view inside a notebook sheet to account.invoice.supplier.form that looks like this:

<field name="x_stock_move" widget="many2many" options="{'no_create': True}" domain="['&amp;',
  ('state','=','done'), ('picking_partner_id','=',context.get('partner_id'))]" 
  attrs="{'readonly':[('state','not in',('draft',))]}">
  <tree>
    <field name="state" invisible="1"/> 
    <field name="date" />
    <field name="picking_partner_id" invisible="1"/>
    <field name="reference" />
    <field name="product_id" />
    <field name="product_uom_qty" string="Cantidad" />
    <field name="product_uom" />
  </tree>
</field>

account.invoice has x_stock_move, a one2many field related to an invoice via x_invoice_id which is many2one in stock.move

My problem comes when (in account.invoice.supplier.form field x_stock_move) I click "add an element": I can see all the stock.moves whose status is "done" and have the same partner as the one selected in the invoice but I can also select the stock.moves that are assigned to another invoice and this should not be possible.

I've tryied adding domain [('x_invoice_id','=','False')] to the field x_stock_move but this modifies the tree view inside account.invoice.supplier.form and not the "add an element" form, this way I can't add stock moves to invoices.

Adding context="{'x_invoice_id':'False'}" or False doesn't seem to change anything.

What should I do to let a user only pick the stock.moves that dont have an x_invoice_id associated inside account.invoice.supplier.form?

And here for part 2:

How can I add each record from x_stock_move as an invoice_line_id in the invoice I'm creating? (I'd rather to do this just editing the xml view account.invoice.supplier.form or without having to develop a custom module)

Thanks for reaching this far and hope you have a nice day :)

To simplify a bit I've ended up developing a module, here is my model description:

class Move(models.Model):
    _inherit = 'stock.move'


    x_invoice_id = fields.Many2one('account.invoice',
        string="Factura de referencia", ondelete='set null')


class Invoice(models.Model):
    _inherit = 'account.invoice'

    x_stock_move = fields.One2many('stock.move',
        string="Movimiento asociado",'x_invoice_id')

How can I add each x_stock_move.product_id and x_stock_move.product_qty as an invoice line?

1
Part 1 can be solved by just checking how to properly compose chained domains domain="['&amp;', ('state','=','done'), ('picking_partner_id','=',context.get('partner_id')), '&amp;', ('x_invoice_id','=',False)]"Nacho R
Can you please add the field defnition of x_stock_move and x_invoice_id python code here? You're mixing "many2many" and "one2many" in your question a lot. Wouldn't be better to create the process from "the other side"? I mean creating invoices out of moves (list) or even from whole pickings.CZoellner
I'm not doing this by developing a module, I just created two new columns in the models account.invoice has field x_stock_move type one2many object stock.move rel x_invoice_id stock.move has field x_invoice_id type many2one object account.invoiceNacho R
creating invoices out of moves sounds like a viable idea for part 2, I will try to find a solution through those linesNacho R
Your requirments will depend on a custom module, just saying.CZoellner

1 Answers

0
votes

You can do this by a wizard that takes the active_id of the invoice when clicking on an action to open the wizard. See my below example (Not Tested but you will get the concept)

stock_move_invoice_wizard.xml

<record model="ir.ui.view" id="stock_move_invoice_wizard">
    <field name="name">Stock Move Invoice</field>
    <field name="model">stock.move.invoice.wizard</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
        <form string="Stock Move Invoice">
            <group>
                <field name="invocie_id"/>
            </group>
            <group>
                <field name="moves_ids"/>
            </group>
            <footer>
                <button name="action_update_moves_date" string="Link moves to invocie" type="object" class="oe_highlight"/>
                <button string="Cancel" special="cancel"/>
            </footer>
        </form>
    </field>
</record>

and in the same file add the action to call the wizard

<act_window id="action_stock_move_invoice_wizard"
            name="Stock Move Invoice"
            res_model="stock.move.invoice.wizard"
            context="{'default_invoice_id': active_id}"
            view_mode="form"
            target="new"/>

stock_move_invoice_wizard.py

class StockMoveInvoiceWizard(models.TransientModel):
    _name = 'stock.move.invoice.wizard'

    invoice_id = fields.Many2one('account.invoice', readonly=True)
    moves_ids = fields.Many2many('stock.move')

    def action_update_moves_invoice(self):
        for rec in self:
            for move in rec.moves_ids:
                move.x_invoice_id = rec.invoice_id.id

account_invoice.xml

<record id="account_invoice_form_inherit" model="ir.ui.view">
    <field name="name">account.invoice.form.inherit</field>
    <field name="model">account.invoice</field>
    <field name="inherit_id" ref="put_here_the_move_view_external_id"/>
    <field name="arch" type="xml">
        <xpath expr="//button[@name='action_cancel']" position="after">
            <button name="%(your_module_external_id.action_stock_move_invoice_wizard)d" string="Link Stock Moves" type="action"/>
        </xpath>
    </field>
</record>