483 lines
21 KiB
Python
483 lines
21 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/>.
|
|
#
|
|
##############################################################################
|
|
import re
|
|
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')
|
|
weight_total = fields.Float(string='Gesamtgewicht', compute='_compute_weight_total')
|
|
confirmation_nr = fields.Char('Freigabenummer')
|
|
|
|
@api.multi
|
|
@api.onchange('partner_invoice_id')
|
|
def _onchange_partner_invoice_id(self):
|
|
for record in self:
|
|
if record.partner_invoice_id.retailer:
|
|
record.payment_term_id = record.partner_invoice_id.property_payment_term_id
|
|
|
|
@api.multi
|
|
def _compute_weight_total(self):
|
|
for record in self:
|
|
_sum = 0
|
|
for line in record.order_line:
|
|
_sum += line.weight * line.product_uom_qty
|
|
record.weight_total = _sum
|
|
|
|
@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):
|
|
"""
|
|
SST-4
|
|
:param line:
|
|
:param state:
|
|
:param limit:
|
|
:return:
|
|
"""
|
|
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):
|
|
"""
|
|
SST-3
|
|
:param vals:
|
|
:return:
|
|
"""
|
|
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.parent_id:
|
|
raise ValidationError(
|
|
_('Zu dem Kontakt mit der portal-ID %s existiert kein Unternehmen') % vals['portal_id'])
|
|
partner = partner.parent_id
|
|
vals['partner_id'] = partner.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)
|
|
payment_term = partner.property_payment_term_id
|
|
if partner.retail_partner_id:
|
|
payment_term = partner.retail_partner_id.property_payment_term_id
|
|
vals.update({
|
|
'partner_id': partner.id,
|
|
'fiscal_position_id': partner.property_account_position_id.id,
|
|
'user_id': partner.user_id.id,
|
|
'payment_term_id': payment_term.id,
|
|
'partner_shipping_id': delivery_partner.id,
|
|
'incoterm': partner.sale_incoterm_id.id,
|
|
'carrier_id': partner.property_delivery_carrier_id.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'],
|
|
'weight': vals['lot_id'].get('weight'),
|
|
'image': vals['lot_id'].get('image'),
|
|
'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',
|
|
'assembly_state', 'confirmation_nr']
|
|
|
|
@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 unlink(self):
|
|
for record in self:
|
|
for line in record.order_line:
|
|
line.lot_id.unlink()
|
|
return super(SaleOrder, self).unlink()
|
|
|
|
@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()]
|
|
|
|
@api.multi
|
|
def action_confirm(self):
|
|
# change name on order confirmation
|
|
if self.name.startswith('ATOF'):
|
|
new_name = re.sub(r"^ATOF", "ATOC", self.name)
|
|
self.name = new_name
|
|
return super(SaleOrder, self).action_confirm()
|
|
|
|
@api.model
|
|
def _formatLang(self, value, currency=True):
|
|
lang = self.partner_id.lang
|
|
lang_objs = self.env['res.lang'].search([('code', '=', lang)])
|
|
if not lang_objs:
|
|
lang_objs = self.env['res.lang'].search([], limit=1)
|
|
lang_obj = lang_objs[0]
|
|
|
|
res = lang_obj.format('%.' + str(2) + 'f', value, grouping=True, monetary=True)
|
|
currency_obj = self.currency_id
|
|
|
|
if currency_obj and currency_obj.symbol and currency:
|
|
if currency_obj.position == 'after':
|
|
res = '%s %s' % (res, currency_obj.symbol)
|
|
elif currency_obj and currency_obj.position == 'before':
|
|
res = '%s %s' % (currency_obj.symbol, res)
|
|
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)])
|
|
weight = fields.Float(string='Gewicht', compute='_compute_weight')
|
|
intrastat_id = fields.Many2one(comodel_name='report.intrastat.code', string='Intrastat Code',
|
|
compute="_compute_intrastat_id")
|
|
|
|
@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
|
|
|
|
@api.multi
|
|
def action_show_lot(self):
|
|
self.ensure_one()
|
|
action = self.env.ref('stock.action_production_lot_form').read()[0]
|
|
action['res_id'] = self.lot_id.id
|
|
action['view_mode'] = 'form'
|
|
action['views'] = [(False, 'form')]
|
|
|
|
return action
|
|
|
|
@api.multi
|
|
def _compute_weight(self):
|
|
for record in self:
|
|
record.weight = record.lot_id.weight or record.product_id.weight
|
|
|
|
@api.multi
|
|
def _compute_intrastat_id(self):
|
|
for record in self:
|
|
record.intrastat_id = record.lot_id.intrastat_id.id or record.product_id.intrastat_id.id
|
|
|
|
@api.multi
|
|
def _prepare_invoice_line(self, qty):
|
|
self.ensure_one()
|
|
res = super(SaleOrderLine, self)._prepare_invoice_line(qty)
|
|
|
|
res['lot_id'] = self.lot_id.id
|
|
return res
|
|
|
|
@api.multi
|
|
def invoice_line_create(self, invoice_id, qty):
|
|
"""
|
|
Overwritten and added a logic to create an extra line for discounts from a retailer
|
|
:param invoice_id:
|
|
:param qty:
|
|
:return:
|
|
"""
|
|
invoice_lines = self.env['account.invoice.line']
|
|
precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
|
|
for line in self:
|
|
if not float_is_zero(qty, precision_digits=precision):
|
|
vals = line._prepare_invoice_line(qty=qty)
|
|
vals.update({'invoice_id': invoice_id, 'sale_line_ids': [(6, 0, [line.id])]})
|
|
invoice_lines |= self.env['account.invoice.line'].create(vals)
|
|
if line.order_id.partner_invoice_id.retailer:
|
|
discount = line.calc_discount()
|
|
discount = int(round(discount))
|
|
if discount > 0:
|
|
vals.update({
|
|
'price_unit': -invoice_lines.price_subtotal * (discount / 100),
|
|
'uom_id': self.env.ref('product.product_uom_unit').id,
|
|
'name': 'Händlerrabatt {}%'.format(discount),
|
|
'intrastat_id': False
|
|
})
|
|
del vals['discount']
|
|
invoice_lines |= self.env['account.invoice.line'].create(vals)
|
|
return invoice_lines
|
|
|
|
@api.multi
|
|
def calc_discount(self):
|
|
discount = 0.0
|
|
context_partner = dict(self.env.context, partner_id=self.order_id.partner_invoice_id.id,
|
|
date=self.order_id.date_order)
|
|
pricelist_context = dict(context_partner, uom=self.product_uom.id)
|
|
|
|
pricelist_id = self.order_id.partner_invoice_id.property_product_pricelist
|
|
price, rule_id = pricelist_id.with_context(pricelist_context).get_product_price_rule(
|
|
self.product_id, self.product_uom_qty or 1.0, self.order_id.partner_invoice_id)
|
|
new_list_price, currency_id = self.with_context(context_partner)._get_real_price_currency(self.product_id,
|
|
rule_id,
|
|
self.product_uom_qty,
|
|
self.product_uom,
|
|
pricelist_id.id)
|
|
|
|
if new_list_price != 0:
|
|
if pricelist_id.currency_id.id != currency_id:
|
|
# we need new_list_price in the same currency as price, which is in the SO's pricelist's currency
|
|
new_list_price = self.env['res.currency'].browse(currency_id).with_context(context_partner).compute(
|
|
new_list_price, pricelist_id.currency_id)
|
|
discount = (new_list_price - price) / new_list_price * 100
|
|
return discount
|