This question has been flagged
2 Replies
2828 Views

This is my code---

I want to visible this button based on payment method(journal_id) from Supplier Payment like If it is a Cash then ,this button should visible,otherwise(if its a bank not visible this button ),How Its Possible,need a help...

====================================================================================================

from osv import osv,fields
from openerp.tools.translate import _

class customer_payments(osv.osv):
    _inherit='account.voucher'

    def _amount_all(self, cr, uid, ids, name, args, context=None):
        res = {}
        for invoice in self.browse(cr, uid, ids, context=context):
            res[invoice.id] = {
                'amount_total': 0.0
            }
            for line in invoice.line_dr_ids:
                res[invoice.id]['amount_total'] += line.amount
        return res

def cash_payment_popup(self, cr, uid, ids, context=None):

        if not ids: return []
        dummy, view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid,'customer_payments', 'customer_payment_wizard_form_view')
        
        inv = self.browse(cr, uid, ids[0], context=context)
        return {
            'name':_("Customer Payments"),
            'view_mode': 'form',
            'view_id': view_id,
            'view_type': 'form',
            'res_model': 'customer.payment.wizard',
            'type': 'ir.actions.act_window',
            'nodestroy': True,
            'target': 'new',
            'domain': '[]',
            'context': {
                'default_supplier_name': self.pool.get('res.partner')._find_accounting_partner(inv.partner_id).id,
                'default_amount': inv.amount_total,
                'default_date': inv.date,
             #   'default_currency': self.pool.get('account.invoice')._get_currency(inv.currency_id).id,
             #   'default_company_name': self.pool.get('res.partner')._find_accounting_partner(inv.company_id).id,
                'default_cash_payment_order_no':inv.reference,
                'default_document': inv.document,
                'default_date_location': inv.date_location,
                'default_purpose': inv.purpose,
                'default_enclosure': inv.enclosure,
           #     'default_amount_words':inv.amount_words,
           #     'default_accounts':inv.ren_period,
                        }
        }

customer_payments()

==============================================================================

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
            <record id="supplier_payment_inherited" model="ir.ui.view">
                <field name="name">supplier.payment.view.form</field>
                <field name="model">account.voucher</field>
                <field name="type">form</field>
                <field name="inherit_id" ref="account_voucher.view_vendor_payment_form"/>
                <field name="arch" type="xml">
            <xpath expr="/form/sheet/field[3]" position="after">
    <button name="cash_payment_popup" string="Cash Payment Order" type="object" class="oe_right oe_highlight"/>
            </xpath>
                    <xpath expr="/form/sheet/group/group[1]/field[4]" position="after">
                        <field name="date_location"/>
                        <field name="enclosure"/>
                    </xpath>
                    <xpath expr="/form/sheet/group/group[2]/field[4]" position="after">
                        <field name="document"/>
                        <field name="purpose"/>
                    </xpath>
                </field>
            </record>
                                                        
    </data>
</openerp>

 

 

Avatar
Discard

It may be resolved by additional boolean field and onchange method of journal_id. for complete solution please refer to my answer below.

Best Answer

Hello,

Try following changes:

# .PY========

...

class customer_payments(osv.osv):
    _name='account.voucher'
    _inherit='account.voucher'

    _columns = {
            'cash_payment_popup_visible': fields.boolean('Cash Popup Visible'),
    }
    _defaults = {
            'cash_payment_popup_visible': False,
    }

    def onchange_journal(self, cr, uid, ids, journal_id, line_ids, tax_id, partner_id, date, amount, ttype, company_id, context=None):
        res = super(customer_payments, self).onchange_journal(cr, uid, ids, journal_id, line_ids, tax_id, partner_id, date, amount, ttype, company_id, context)
        if res and journal_id:
            account_journal_obj = self.pool.get('account.journal').browse(cr,uid,journal_id, context)
            if account_journal_obj.type == 'cash':
                res['value']['cash_payment_popup_visible'] = True
        elif not res:
            res = {'value':{}}
        res['value']['cash_payment_popup_visible'] = res['value'].get('cash_payment_popup_visible', False)
        return res


    def _amount_all(self, cr, uid, ids, name, args, context=None):
        res = {}
...

 

# .XML=======

...

<xpath expr="/form/sheet/field[3]" position="after">
    <button name="cash_payment_popup" string="Cash Payment Order" type="object" class="oe_right oe_highlight" attrs="{'invisible':[('cash_payment_popup_visible','=',False)]}" />
    <field name="cash_payment_popup_visible"  invisible="1" />
</xpath>

...

 

regards,

Avatar
Discard
Author

Thanks Temur... will chk

If it worked for you, please mark this answer as accepted.

Best Answer

What have you tried to make it invisible? 

Have you tried using the "attrs" tag in the XML code?

Avatar
Discard
Author

How to write XML for this in journal_id .(if its a cash then the button visible),How to write .I tried but effect

Author

If journal_id is cash(from supplier payement ) then only my custom button(cash_payment_popup) should visible

Did you check the original source for attrs in xml? If so, post you code in the original question and someone will check what was wrong with it.

Author

Yes I Checked,It resolved Thx Ludo..4 rply