i try to create sale order from postman
the sale order with custom field attachment :
attachment = fields.Binary("Attach")
the sale order is created whit-out the attachment
this is my code :
#route('/order', csrf=False, type='http', auth="public", methods=['POST'])
def create_order(self, **kwargs):
partner_id = kwargs['partner_id']
attach = kwargs['attach']
sale = request.env['sale.order'].sudo().create({
'partner_id': partner_id,
'attachment': base64.encodebytes(attach.read()),
})
Related
I have inherited account.move model and added job_card_id field(many2one) in it, as shown as below :
Below given is Image of Selected Job Card :
Below given is code of inherited model :
class CustomInvoice(models.Model):
_inherit = "account.move"
job_card_id = fields.Many2one('job.card', string="Job Card", domain="[('customer_id','=',partner_id)]", tracking=True)
Below given is code of Job Card Line :
class JobCardLine(models.Model):
_name = "job.card.line"
job_card_id = fields.Many2one('job.card', string="Job Card Id", tracking=True)
product_id = fields.Many2one('product.template', string="Product", tracking=True)
quantity = fields.Integer(string="Quantity", tracking=True)
price = fields.Float(
'Sales Price',
digits='Product Price')
total = fields.Integer(string='Total', compute='_compute_total', tracking=True,
help="This field will be calculated from dob !")
employee_id = fields.Many2one('hr.employee', string="Employee", tracking=True)
Now I wanted to add product line of selected job card into Invoice product line automatically when I select the job card.
You need to create an onchange method depending on the job_card_id.
In this onchange, you will add a line to invoice_line_ids.
This will triggers all the other onchange and compute methods and set ups everything needed.
Here is a small example:
class CustomInvoice(models.Model):
_inherit = "account.move"
job_card_id = fields.Many2one('job.card', string="Job Card", domain="[('customer_id','=',partner_id)]", tracking=True)
#api.onchange('job_card_id')
def _onchange_job_card_id(self):
# creates your invoice lines vals according to your values
invoice_lines_vals = self.job_card_id.get_invoice_line_vals()
self.write({'invoice_line_ids': [(6, 0, 0)] + [(0, 0, vals) for vals in invoice_lines_vals]})
The 6 is a command that will deletes all previous invoice_line and the 0 is a creation command.
more info on ORM command
Now, you just need to create the get_invoice_line_vals method in your JobCard class.
It should return a list of vals.
Each vals should be a dict containing some information such as the quantity and the price_unit.
It depends mostly on how you want your data to be calculate.
You just need to return a list like this:
def get_invoice_line_vals(self):
vals_list = []
for job_card_line in self.line_ids:
vals_list.append({
'price_unit': job_card_line.price_unit,
'quantity': job_card_line.quantity
})
return vals_list
So I have a Sale model and SaleLine model. Sale model have a field sale_line_ids as One2many from SaleLine model.
Sale
class Sale(models.Model):
_name = 'store.sale'
_description = 'Store Sale'
...
sale_line_ids = fields.One2many('store.sale_line', 'sale_id', string='Sale Lines')
...
Sale Line
class SaleLine(models.Model):
_name = 'store.sale_line'
_description = 'Store Sale Line'
sale_id = fields.Many2one('store.sale', string='Sale Reference', ondelete='cascade', index=True)
product_id = fields.Many2one('store.product', string='Product', required=True)
quantity = fields.Integer(string='Quantity', required=True)
I want to create SaleLine model programmatically and add that model to sale_line_ids field. How can I do that?
This answer is what I actually want to implement. However, the model is immediately saved to a database. I need to create a SaleLine model using env[].create({}) method.
self.env['store.sale_line'].create({
'sale_id': rec.id,
'product_id': id_from_another_model,
'quantity': quantity_from_another_model,
})
After that, I need to commit in order to save the data in a database.
self.env.cr.commit()
UPDATE
The previous answer required record to store directly. The best answer to solve the problem is to create temporary record that only saved when user click the save button.
Syntax
(0, 0, { values })
First create sale_line list
sale_line = []
for data in list_of_data:
sale_line.append([0, 0, {
'sale_id': data['sale_id'],
'product_id': data['product_id'],
'quantity': data['quantity'],
}])
Assign sale_line list to sale_line_ids field
self.write({'sale_line_ids': sale_line})
And override the create method to commit the change
#api.model
def create(self, values):
self.env.cr.commit() # This is the important part
return super(Sale, self).create(values)
I want to create a record on inventory with quantity = 0 whenever I add a new record to products. Is it possible?
I have 2 modules like so :
class productss(models.Model):
_name = 'productss.productss'
name = fields.Char()
description = fields.Text()
price =fields.Float()
class inventory(models.Model):
_name = 'inventory.inventory'
id_product = fields.Many2one(comodel_name='productss.productss')
qte = fields.Integer(compute="_value_qte", store=True)
Yes, it is possible. Just override productss.productss's create() and create an inventory record within:
#api.model
def create(self, values)
record = super().create(values)
# create an inventory
inv_values = {'id_product': record.id, 'qte': 0}
self.env['inventory.inventory'].create(inv_values)
return record
This code requires the "new" Odoo API (V8+) and Python3
This button
<span class="o_stat_value"><field name="test_field"/>Material Out</span>
class SkylineJobOrder(models.Model):
_name = 'skyline.job.order'
_inherit = 'purchase.order'
#api.multi
def action_workorder_out(self):
x = 0
""" This opens the xml view specified in xml_id for the current Work Order in Manufacturing """
self.ensure_one()
xml_id = self.env.context.get('xml_id')
if xml_id:
res = self.env['ir.actions.act_window'].for_xml_id('stock', xml_id)
production = self.env['mrp.production'].search([('product_id','=',self.product_id.id)])
for item in production:
x = item.bom_id.id
bomline = self.env['mrp.bom.line'].search([('bom_id','=',x)])
for record in bomline:
res.update(
context={'default_states':'draft','default_partner_id': self.partner_id.id,'default_move_lines': [(0,0, {'address_in_id':self.partner_id.id,'product_id':record.product_id.id,'product_uom': record.product_uom_id.id,'product_uom_qty':record.product_qty,'scrapped':False,'state':'draft','picking_id':False,'name': 'test','procurement_id': record.operation_id.id,'split_from': self.id,'no_open': True,'no_create':True,'availability':1,'location_dest_id':8,'picking_id':35})]}
)
print res
return res
return False
I inherit purchase.order and create a button and put action of
stock.picking . The working of button is to stockout the values of raw
material of product .so when click on save button is shows that the
following fields are invalid stock.move.
Blockquote
value inserted form bom.line.ids to stock.move through stock.picking using picking_id, location_id,lication_dest_id and 'purchase=ok, in purchase_id.
Partner ledger report name is 'account.report_partnerledger.pdf' by default. I want to change it to customer name (eg: john.pdf if customer name is john). How to do this?
Install report_custom_filename.
Go to Settings > Actions > Reports and search for Partner Ledger.
Fill in the Download filename field. This field is evaluated as jinja2 template with objects being a list of browse records of the records to print, and o the first record. If your model contains a name field, you might write something like ${o.name}_report.pdf as filename.
Possible through complex coding but thanks to odoo community we have one module named
report_custom_filename
which will let you do this by little configuration
Install 'report_custom_filename' module and make the following changes in report_routes method
def report_routes(self, reportname, docids=None, converter=None, **data):
cr, uid, context,registry = request.cr, request.uid, request.context,request.registry
response = super(ReportController, self).report_routes(
reportname, docids=docids, converter=converter, **data)
if docids:
docids = [int(i) for i in docids.split(',')]
report_xml = http.request.session.model('ir.actions.report.xml')
report_ids = report_xml.search(
[('report_name', '=', reportname)])
options_data = simplejson.loads(data['options'])
partner_id = options_data.get('ids')
for report in report_xml.browse(report_ids):
if not report.download_filename:
continue
#objects = http.request.session.model(report.model).browse(docids or [])
objects = request.registry[report.model].browse(cr, uid, partner_id, context=context)
customer_name = str(objects.name)
generated_filename = email_template.mako_template_env\
.from_string(report.download_filename)\
.render({
'objects': objects,
'o': customer_name,
'object': objects[:1],
'ext': report.report_type.replace('qweb-', ''),
})
response.headers['Content-Disposition'] = content_disposition(
generated_filename)
return response