236 lines
9.8 KiB
Python
236 lines
9.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
##############################################################################
|
|
#
|
|
# datenpol gmbh
|
|
# Copyright (C) 2013-TODAY datenpol gmbh (<http://www.datenpol.at/>)
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
##############################################################################
|
|
from datetime import datetime
|
|
|
|
from odoo import api, fields, models, _
|
|
from odoo.exceptions import ValidationError
|
|
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
|
|
|
|
|
|
class SaleOrder(models.Model):
|
|
_name = 'sale.order'
|
|
_inherit = ['sale.order', 'dp_custom.helper']
|
|
|
|
ASSEMBLY_STATES = [('approved', 'Produktionsfreigabe'),
|
|
('started', 'Produktion begonnen'),
|
|
('done', 'Produktion fertig'),
|
|
('packed', 'Verpackt'),
|
|
('delivered', 'Geliefert')]
|
|
|
|
assembled = fields.Boolean(string='Zusammengebaut')
|
|
line_id = fields.Many2one(comodel_name='res.line', string='Produktionslinie')
|
|
assembly_state = fields.Selection(ASSEMBLY_STATES, string="Status PG", track_visibility='onchange')
|
|
quote_name = fields.Char(compute='_compute_quote_name')
|
|
internal_notes = fields.Text()
|
|
assembly_notes = fields.Text()
|
|
|
|
@api.multi
|
|
def _compute_quote_name(self):
|
|
for record in self:
|
|
if record.state in ['draft', 'sent']:
|
|
record.quote_name = record.name.replace('ATOC', 'ATOF')
|
|
else:
|
|
record.quote_name = record.name
|
|
|
|
@api.model
|
|
def pg_get_orders(self, line, state, limit):
|
|
line_id = self.env['res.line'].search([('name', '=', line)])
|
|
orders = self.search([('line_id', '=', line_id.id), ('assembly_state', '=', state)], order='id ASC',
|
|
limit=limit)
|
|
order_list = []
|
|
for order in orders:
|
|
attachmets = self.env['ir.attachment'].search([('res_model', '=', 'sale.order'), ('res_id', '=', order.id)])
|
|
attachment_list = []
|
|
for attachment in attachmets:
|
|
attachment_list.append({
|
|
'filename': attachment.name,
|
|
'binary': attachment.datas.decode()
|
|
})
|
|
delivery_date = False
|
|
for picking_id in order.picking_ids:
|
|
if not delivery_date:
|
|
delivery_date = picking_id.scheduled_date
|
|
elif datetime.strptime(picking_id.scheduled_date, DEFAULT_SERVER_DATETIME_FORMAT) < datetime.strptime(
|
|
delivery_date, DEFAULT_SERVER_DATETIME_FORMAT):
|
|
delivery_date = picking_id.scheduled_date
|
|
|
|
order_list.append({
|
|
'id': order.id,
|
|
'name': order.name,
|
|
'attachments': attachment_list,
|
|
'internal_notes': order.internal_notes,
|
|
'assembly_notes': order.assembly_notes,
|
|
'user_id': order.user_id.name,
|
|
'delivery_date': delivery_date
|
|
})
|
|
return order_list
|
|
|
|
@api.model
|
|
def pg_create_quotation(self, vals):
|
|
if not vals.get('portal_id', False):
|
|
raise ValidationError(
|
|
_("Der Kunde mit der Portal-ID \'%s\' kann nicht zugeordnet werden" % vals['portal_id']))
|
|
partner = self.env['res.partner'].search([('portal_id', '=', vals['portal_id'])])
|
|
if not partner:
|
|
raise ValidationError(
|
|
_("Der Kunde mit der Portal-ID \'%s\' kann nicht zugeordnet werden" % vals['portal_id']))
|
|
delivery_partner = self.env['res.partner']
|
|
delivery_vals = {}
|
|
if vals.get('portal_delivery_id', False):
|
|
delivery_partner = self.env['res.partner'].search([('portal_id', '=', vals['portal_delivery_id'])])
|
|
delivery_vals['portal_id'] = vals['portal_delivery_id']
|
|
for key in list(vals.keys()):
|
|
if key.startswith('delivery_'):
|
|
delivery_vals[key.replace('delivery_', '')] = vals[key]
|
|
|
|
delivery_vals = delivery_partner.with_context(sst_1=True).remove_not_specified_fields(delivery_vals)
|
|
delivery_vals = delivery_partner.correct_values(delivery_vals)
|
|
delivery_vals['parent_id'] = partner.id
|
|
|
|
if delivery_partner:
|
|
delivery_partner.write(delivery_vals)
|
|
else:
|
|
if not delivery_vals.get('type', False):
|
|
delivery_vals['type'] = 'delivery'
|
|
delivery_partner = delivery_partner.create(delivery_vals)
|
|
|
|
attachment_vals = vals.get('attachment_ids', False)
|
|
order_line_vals = vals.get('order_lines', False)
|
|
vals = self.remove_not_specified_fields(vals)
|
|
vals = self.correct_values(vals)
|
|
vals.update({
|
|
'partner_id': partner.id,
|
|
'fiscal_position_id': partner.property_account_position_id.id,
|
|
'user_id': partner.user_id.id,
|
|
'payment_term_id': partner.property_payment_term_id.id,
|
|
'partner_shipping_id': delivery_partner.id,
|
|
'partner_invoice_id': partner.id
|
|
})
|
|
order_id = self.create(vals)
|
|
if attachment_vals:
|
|
order_id.pg_create_sale_order_attachments(attachment_vals)
|
|
if order_line_vals:
|
|
order_id.pg_create_order_lines(order_line_vals)
|
|
return {'id': order_id.id, 'name': order_id.name}
|
|
|
|
@api.multi
|
|
def pg_create_sale_order_attachments(self, values):
|
|
self.ensure_one()
|
|
if isinstance(values, list):
|
|
for vals in values:
|
|
self.create_attachment(self, vals)
|
|
else:
|
|
self.create_attachment(self, values)
|
|
|
|
@api.multi
|
|
def pg_create_order_lines(self, values):
|
|
order_lines = []
|
|
for vals in values:
|
|
vals = self.env['sale.order.line'].correct_values(vals)
|
|
|
|
lot_id = False
|
|
if vals.get('lot_id', False):
|
|
if vals['lot_id'].get('attachment_ids', False):
|
|
lot_attachment_values = vals['lot_id']['attachment_ids']
|
|
else:
|
|
lot_attachment_values = []
|
|
lot = self.env['stock.production.lot'].create({
|
|
'name': vals['lot_id']['name'],
|
|
'product_id': vals['product_id'],
|
|
'notes': vals['lot_id']['notes']
|
|
})
|
|
for lot_attachment_vals in lot_attachment_values:
|
|
self.create_attachment(lot, lot_attachment_vals)
|
|
lot_id = lot.id
|
|
order_lines.append(self.env['sale.order.line'].create({
|
|
'order_id': self.id,
|
|
'name': vals['name'],
|
|
'product_id': vals['product_id'],
|
|
'price_unit': vals['price_unit'],
|
|
'product_uom_qty': vals['product_uom_qty'],
|
|
'lot_id': lot_id,
|
|
'from_designbox': True,
|
|
}))
|
|
return order_lines
|
|
|
|
@api.model
|
|
def create_attachment(self, record, vals):
|
|
attachment_vals = {
|
|
'name': vals['filename'],
|
|
'datas': vals['binary'],
|
|
'from_designbox': True,
|
|
'datas_fname': vals['filename'],
|
|
'res_model': record._name,
|
|
'res_id': record.id,
|
|
}
|
|
self.env['ir.attachment'].create(attachment_vals)
|
|
|
|
@api.model
|
|
def correct_values(self, vals):
|
|
if vals.get('line_id', False):
|
|
line_id = self.env['res.line'].search([('name', '=', vals['line_id'])])
|
|
if line_id:
|
|
vals['line_id'] = line_id.id
|
|
else:
|
|
raise ValidationError(
|
|
_("Produktionslinie \'%s\' kann nicht zugeordnet werden" % vals['line_id']))
|
|
return vals
|
|
|
|
@api.model
|
|
def _get_specified_fields(self):
|
|
return ['origin', 'client_order_ref', 'note', 'date_order', 'assembled', 'line_id', 'partner_id',
|
|
'fiscal_position_id', 'user_id', 'payment_term_id', 'partner_delivery_id', 'partner_invoice_id']
|
|
|
|
@api.multi
|
|
def write(self, vals):
|
|
res = super(SaleOrder, self).write(vals)
|
|
if vals.get('assembly_state', False) and vals.get('assembly_state', False) == 'done':
|
|
self.message_post(body='Produktion fertig')
|
|
|
|
return res
|
|
|
|
|
|
class SaleOrderLine(models.Model):
|
|
_inherit = 'sale.order.line'
|
|
|
|
lot_id = fields.Many2one(comodel_name='stock.production.lot', string='Lot')
|
|
from_designbox = fields.Boolean(string='Import von Designbox', readonly=True)
|
|
product_id = fields.Many2one(domain=[('sale_ok', '=', True), ('can_be_sold_unconfigured', '=', True)])
|
|
|
|
@api.multi
|
|
def write(self, vals):
|
|
for record in self:
|
|
if record.from_designbox and set(vals.keys()).intersection(
|
|
['product_uom_qty', 'product_uom', 'price_unit']):
|
|
raise ValidationError(_("Menge und Preis können von Produkten aus der Designbox nicht geändert werden"))
|
|
return super(SaleOrderLine, self).write(vals)
|
|
|
|
@api.model
|
|
def correct_values(self, vals):
|
|
if vals.get('product_id', False):
|
|
product_id = self.env['product.product'].search([('default_code', '=', vals['product_id'])])
|
|
if product_id:
|
|
vals['product_id'] = product_id.id
|
|
else:
|
|
raise ValidationError(
|
|
_("Produkt \'%s\' kann nicht zugeordnet werden" % vals['product_id']))
|
|
return vals
|