324 lines
14 KiB
Python
324 lines
14 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.tools import float_is_zero
|
|
from odoo.exceptions import ValidationError, UserError
|
|
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
|
|
|
|
|
|
class SaleOrder(models.Model):
|
|
_name = 'sale.order'
|
|
_inherit = ['sale.order', 'dp_custom.helper']
|
|
|
|
ASSEMBLY_STATES = [('created', 'nicht freigegeben'),
|
|
('approved', 'Produktionsfreigabe'),
|
|
('wait', 'freigegeben'),
|
|
('failed', 'Fehler Freigabe'),
|
|
('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()
|
|
earliest_scheduled_date = fields.Datetime(compute='_compute_earliest_scheduled_date')
|
|
positions = fields.Integer(string='Positionen', compute='_compute_positions')
|
|
num_items = fields.Integer(string='Anzahl der Artikel', compute='_compute_num_items')
|
|
|
|
@api.multi
|
|
def _compute_positions(self):
|
|
for record in self:
|
|
record.positions = len(record.order_line)
|
|
|
|
@api.multi
|
|
def _compute_num_items(self):
|
|
for record in self:
|
|
num_items = 0
|
|
for line in record.order_line:
|
|
if line.product_uom == self.env.ref('product.product_uom_unit'): # wenn die Mengeneinheit Stk. ist
|
|
num_items += line.product_uom_qty
|
|
record.num_items = num_items
|
|
|
|
@api.multi
|
|
def _compute_earliest_scheduled_date(self):
|
|
for record in self:
|
|
earliest_scheduled_date = False
|
|
for picking in record.picking_ids:
|
|
if not earliest_scheduled_date or earliest_scheduled_date > fields.Datetime.from_string(
|
|
picking.scheduled_date):
|
|
earliest_scheduled_date = fields.Datetime.from_string(picking.scheduled_date)
|
|
if earliest_scheduled_date:
|
|
record.earliest_scheduled_date = earliest_scheduled_date
|
|
|
|
@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
|
|
|
|
@api.multi
|
|
def action_invoice_create(self, grouped=False, final=False):
|
|
inv_obj = self.env['account.invoice']
|
|
precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
|
|
invoices = {}
|
|
references = {}
|
|
for order in self:
|
|
collective_bill = order.partner_id.collective_bill and "x" or order.id
|
|
group_key = order.id if grouped else (collective_bill, order.partner_invoice_id.id, order.currency_id.id)
|
|
for line in order.order_line.sorted(key=lambda l: l.qty_to_invoice < 0):
|
|
if float_is_zero(line.qty_to_invoice, precision_digits=precision):
|
|
continue
|
|
if group_key not in invoices:
|
|
inv_data = order._prepare_invoice()
|
|
invoice = inv_obj.create(inv_data)
|
|
references[invoice] = order
|
|
invoices[group_key] = invoice
|
|
elif group_key in invoices:
|
|
vals = {}
|
|
if order.name not in invoices[group_key].origin.split(', '):
|
|
vals['origin'] = invoices[group_key].origin + ', ' + order.name
|
|
if order.client_order_ref and order.client_order_ref not in invoices[group_key].name.split(
|
|
', ') and order.client_order_ref != invoices[group_key].name:
|
|
vals['name'] = invoices[group_key].name + ', ' + order.client_order_ref
|
|
invoices[group_key].write(vals)
|
|
if line.qty_to_invoice > 0:
|
|
line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice)
|
|
elif line.qty_to_invoice < 0 and final:
|
|
line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice)
|
|
|
|
if references.get(invoices.get(group_key)):
|
|
if order not in references[invoices[group_key]]:
|
|
references[invoice] = references[invoice] | order
|
|
|
|
if not invoices:
|
|
raise UserError(_('There is no invoicable line.'))
|
|
|
|
for invoice in invoices.values():
|
|
if not invoice.invoice_line_ids:
|
|
raise UserError(_('There is no invoicable line.'))
|
|
# If invoice is negative, do a refund invoice instead
|
|
if invoice.amount_untaxed < 0:
|
|
invoice.type = 'out_refund'
|
|
for line in invoice.invoice_line_ids:
|
|
line.quantity = -line.quantity
|
|
# Use additional field helper function (for account extensions)
|
|
for line in invoice.invoice_line_ids:
|
|
line._set_additional_fields(invoice)
|
|
# Necessary to force computation of taxes. In account_invoice, they are triggered
|
|
# by onchanges, which are not triggered when doing a create.
|
|
invoice.compute_taxes()
|
|
invoice.message_post_with_view('mail.message_origin_link',
|
|
values={'self': invoice, 'origin': references[invoice]},
|
|
subtype_id=self.env.ref('mail.mt_note').id)
|
|
return [inv.id for inv in invoices.values()]
|
|
|
|
|
|
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
|