Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to set the partner's sale currency in OpenERP (Odoo) ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 836
    Comment on it

    In below example I have written Python script to the partner's sale currency . Use below python code in .py file :

    for order in self.pool.get('pos.order').browse(cr, uid, ids, context=context):
                if order.invoice_id:
                    inv_ids.append(order.invoice_id.id)
                    continue
    
                if not order.partner_id:
                    raise osv.except_osv(_('Error!'), _('Please provide a partner for the sale.'))
    
                acc = order.partner_id.property_account_receivable.id
                inv = {
                    'name': order.name,
                    'origin': order.name,
                    'account_id': acc,
                    'journal_id': order.sale_journal.id or None,
                    'type': 'out_invoice',
                    'reference': order.name,
                    'partner_id': order.partner_id.id,
                    'comment': order.note or '',
                    'currency_id': order.pricelist_id.currency_id.id, # considering partner's sale pricelist's currency
                }
                inv.update(inv_ref.onchange_partner_id(cr, uid, [], 'out_invoice', order.partner_id.id)['value'])
                if not inv.get('account_id', None):
                    inv['account_id'] = acc
                inv_id = inv_ref.create(cr, uid, inv, context=context)
    
                self.write(cr, uid, [order.id], {'invoice_id': inv_id, 'state': 'invoiced'}, context=context)
                inv_ids.append(inv_id)
                for line in order.lines:
                    inv_line = {
                        'invoice_id': inv_id,
                        'product_id': line.product_id.id,
                        'quantity': line.qty,
                    }
                    inv_name = product_obj.name_get(cr, uid, [line.product_id.id], context=context)[0][1]
                    inv_line.update(inv_line_ref.product_id_change(cr, uid, [],
                                                                   line.product_id.id,
                                                                   line.product_id.uom_id.id,
                                                                   line.qty, partner_id = order.partner_id.id,
                                                                   fposition_id=order.partner_id.property_account_position.id)['value'])
                    if line.product_id.description_sale:
                        inv_line['note'] = line.product_id.description_sale
                    inv_line['price_unit'] = line.price_unit
                    inv_line['discount'] = line.discount
                    inv_line['name'] = inv_name
                    inv_line['invoice_line_tax_id'] = [(6, 0, [x.id for x in line.product_id.taxes_id] )]
                    inv_line_ref.create(cr, uid, inv_line, context=context)
                inv_ref.button_reset_taxes(cr, uid, [inv_id], context=context)
                wf_service.trg_validate(uid, 'pos.order', order.id, 'invoice', cr)
    
            if not inv_ids: return {}
    
            mod_obj = self.pool.get('ir.model.data')
            res = mod_obj.get_object_reference(cr, uid, 'account', 'invoice_form')
            res_id = res and res[1] or False
            return {
                'name': _('Customer Invoice'),
                'view_type': 'form',
                'view_mode': 'form',
                'view_id': [res_id],
                'res_model': 'account.invoice',
                'context': "{'type':'out_invoice'}",
                'type': 'ir.actions.act_window',
                'nodestroy': True,
                'target': 'current',
                'res_id': inv_ids and inv_ids[0] or False,
            }
    
        def create_account_move(self, cr, uid, ids, context=None):
            return self._create_account_move_line(cr, uid, ids, None, None, context=context)
    
        def _create_account_move_line(self, cr, uid, ids, session=None, move_id=None, context=None):
            # Tricky, via the workflow, we only have one id in the ids variable
            """Create a account move line of order grouped by products or not."""
            account_move_obj = self.pool.get('account.move')
            account_period_obj = self.pool.get('account.period')
            account_tax_obj = self.pool.get('account.tax')
            property_obj = self.pool.get('ir.property')
            cur_obj = self.pool.get('res.currency')
    
            #session_ids = set(order.session_id for order in self.browse(cr, uid, ids, context=context))
    
            if session and not all(session.id == order.session_id.id for order in self.browse(cr, uid, ids, context=context)):
                raise osv.except_osv(_('Error!'), _('Selected orders do not have the same session!'))
    
            grouped_data = {}
            have_to_group_by = session and session.config_id.group_by or False
    
            def compute_tax(amount, tax, line):
                if amount > 0:
                    tax_code_id = tax['base_code_id']
                    tax_amount = line.price_subtotal * tax['base_sign']
                else:
                    tax_code_id = tax['ref_base_code_id']
                    tax_amount = line.price_subtotal * tax['ref_base_sign']
    
                return (tax_code_id, tax_amount,)
    
            for order in self.browse(cr, uid, ids, context=context):
                if order.account_move:
                    continue
                if order.state != 'paid':
                    continue
    
                current_company = order.sale_journal.company_id
    
                group_tax = {}
                account_def = property_obj.get(cr, uid, 'property_account_receivable', 'res.partner', context=context)
    
                order_account = order.partner_id and \
                                order.partner_id.property_account_receivable and \
                                order.partner_id.property_account_receivable.id or \
                                account_def and account_def.id or current_company.account_receivable.id
    
                if move_id is None:
                    # Create an entry for the sale
                    move_id = account_move_obj.create(cr, uid, {
                        'ref' : order.name,
                        'journal_id': order.sale_journal.id,
                    }, context=context)
    
                def insert_data(data_type, values):
                    # if have_to_group_by:
    
                    sale_journal_id = order.sale_journal.id
                    period = account_period_obj.find(cr, uid, context=dict(context or {}, company_id=current_company.id, account_period_prefer_normal=True))[0]
    
                    # 'quantity': line.qty,
                    # 'product_id': line.product_id.id,
                    values.update({
                        'date': order.date_order[:10],
                        'ref': order.name,
                        'journal_id' : sale_journal_id,
                        'period_id' : period,
                        'move_id' : move_id,
                        'company_id': current_company.id,
                    })
    
                    if data_type == 'product':
                        key = ('product', values['partner_id'], values['product_id'], values['debit'] > 0)
                    elif data_type == 'tax':
                        key = ('tax', values['partner_id'], values['tax_code_id'], values['debit'] > 0)
                    elif data_type == 'counter_part':
                        key = ('counter_part', values['partner_id'], values['account_id'], values['debit'] > 0)
                    else:
                        return
    
                    grouped_data.setdefault(key, [])
    
                    # if not have_to_group_by or (not grouped_data[key]):
                    #     grouped_data[key].append(values)
                    # else:
                    #     pass
    
                    if have_to_group_by:
                        if not grouped_data[key]:
                            grouped_data[key].append(values)
                        else:
                            current_value = grouped_data[key][0]
                            current_value['quantity'] = current_value.get('quantity', 0.0) +  values.get('quantity', 0.0)
                            current_value['credit'] = current_value.get('credit', 0.0) + values.get('credit', 0.0)
                            current_value['debit'] = current_value.get('debit', 0.0) + values.get('debit', 0.0)
                            current_value['tax_amount'] = current_value.get('tax_amount', 0.0) + values.get('tax_amount', 0.0)
                    else:
                        grouped_data[key].append(values)

     

 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: