1032 lines
		
	
	
		
			46 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			1032 lines
		
	
	
		
			46 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.exceptions import ValidationError, UserError, Warning
 | |
| from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
 | |
| from odoo.tools import float_compare
 | |
| from odoo.tools import float_is_zero
 | |
| from odoo.tools import drop_view_if_exists
 | |
| 
 | |
| from dateutil.relativedelta import relativedelta
 | |
| import dateutil.parser
 | |
| 
 | |
| class SaleCart(models.Model):
 | |
|     _name = 'sale.cart'
 | |
|     _auto = False
 | |
| 
 | |
|     ASSEMBLY_STATES = [('import',           'Imported'),
 | |
|                        ('import_failed',    'Error Import'),
 | |
|                        ('created',          'Not Released'),
 | |
|                        ('approved',         'Released for Production'),
 | |
|                        ('wait',             'Released'),
 | |
|                        ('failed',           'Error Release'),
 | |
|                        ('started',          'Production Started'),
 | |
|                        ('done',             'Production Finished'),
 | |
|                        ('packed',           'Packed'),
 | |
|                        ('delivered',        'Delivered'),
 | |
|                        ('completed',        'Completed')]
 | |
| 
 | |
|     order_states = [
 | |
|         ('draft', 'Quotation'),
 | |
|         ('sent', 'Quotation Sent'),
 | |
|         ('sale', 'Sales Order'),
 | |
|         ('done', 'Done'),
 | |
|         ('cancel', 'Cancelled')]
 | |
| 
 | |
|     order_id = fields.Many2one('sale.order', string = 'Quotation / Order')
 | |
|     origin = fields.Char(string='Shopping Cart')
 | |
|     client_order_ref = fields.Char(string='Customer Reference')
 | |
|     name = fields.Char(string='Quotation')
 | |
|     date_order = fields.Date(string='Quotation Date:')
 | |
|     create_date = fields.Datetime(string='Create Date:')
 | |
|     partner_id = fields.Many2one('res.partner', string = 'Customer')
 | |
|     company_id = fields.Many2one('res.company', string = 'Company')
 | |
|     user_id = fields.Many2one('res.users', string = 'User')
 | |
|     assembly_state = fields.Selection(ASSEMBLY_STATES, string="State PG")
 | |
|     state = fields.Selection(order_states, string="State")
 | |
|     in_company = fields.Boolean(default=False,compute='_in_company',store=False)
 | |
| 
 | |
|     @api.multi
 | |
|     def _in_company(self):
 | |
|         sCompany = self.env.user.company_id
 | |
|         for record in self:
 | |
|             if record.company_id == sCompany:
 | |
|                 record.in_company = True
 | |
| 
 | |
|     @api.model_cr
 | |
|     def init(self):
 | |
| #        print("connected")
 | |
|         drop_view_if_exists(self._cr, 'sale_cart')
 | |
|         self._cr.execute("""CREATE OR REPLACE VIEW sale_cart AS 
 | |
|                                 SELECT DISTINCT row_number() OVER () AS id,
 | |
|                                 so.id AS order_id,
 | |
|                                 so.name AS name, 
 | |
|                                 so.origin AS origin,
 | |
|                                 so.client_order_ref AS client_order_ref,
 | |
|                                 so.date_order AS date_order,
 | |
|                                 so.create_date AS create_date,
 | |
|                                 so.assembly_state AS assembly_state,
 | |
|                                 so.state AS state,
 | |
|                                 so.partner_id AS partner_id,
 | |
|                                 so.company_id AS company_id,
 | |
|                                 c_u.user_id AS user_id
 | |
|                                 FROM sale_order so 
 | |
|                                 INNER JOIN res_company_users_rel c_u ON so.company_id = c_u.cid 
 | |
|                                 WHERE so.state != 'cancel' AND so.origin != ''
 | |
|                                 """)
 | |
| 
 | |
|     @api.multi
 | |
|     def switch_company(self):
 | |
|         values = {'company_id': self.company_id.id}
 | |
|         self.env.user.write(values)
 | |
|         return {
 | |
|             'type': 'ir.actions.client',
 | |
|             'tag': 'reload_context',
 | |
|         }
 | |
| 
 | |
| class SaleOrder(models.Model):
 | |
|     _name = 'sale.order'
 | |
|     _inherit = ['sale.order', 'dp_custom.helper']
 | |
| 
 | |
|     ASSEMBLY_STATES = [('import',           'Imported'),
 | |
|                        ('import_failed',    'Error Import'),
 | |
|                        ('created',          'Not Released'),
 | |
|                        ('approved',         'Released for Production'),
 | |
|                        ('wait',             'Released'),
 | |
|                        ('failed',           'Error Release'),
 | |
|                        ('started',          'Production Started'),
 | |
|                        ('done',             'Production Finished'),
 | |
|                        ('packed',           'Packed'),
 | |
|                        ('delivered',        'Delivered'),
 | |
|                        ('completed',        'Completed')]
 | |
| 
 | |
|     # ORDER_TYPES = [
 | |
|     #     ('M', 'Manuell'),
 | |
|     #     ('D', 'DesignBox'),
 | |
|     #     ('I', 'Industrie'),
 | |
|     #     ('T', 'TZBox')
 | |
|     ORDER_TYPES = [
 | |
|         ('M', 'M'),
 | |
|         ('D', 'D'),
 | |
|         ('I', 'I'),
 | |
|         ('T', 'T')
 | |
|     ]
 | |
| 
 | |
|     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', help='Bitte nicht manuell ändern', default='', copy=False)
 | |
|     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')
 | |
|     production_positions = fields.Integer(string='Positionen', compute='_compute_positions', store=False)
 | |
|     assembly_positions = fields.Integer(string='Positionen', compute='_compute_positions', store=False)
 | |
|     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',track_visibility='onchange')
 | |
|     order_type = fields.Selection(ORDER_TYPES, string='Auftragsart', default='M')
 | |
| 
 | |
|     clerk_id = fields.Many2one('res.users', string='Sachbearbeiter',
 | |
|                                domain=[('clerk_name', '!=', '')],track_visibility='onchange')
 | |
|     editor_id = fields.Many2one('res.users', string='Auftragsbearbeiter',
 | |
|                                 default=lambda self: self.env.user if self.env.user.editor_name else '',
 | |
|                                 domain=[('editor_name', '!=', '')],track_visibility='onchange')
 | |
| 
 | |
|     desired_delivery_date = fields.Date(string='Wunschlieferdatum')
 | |
|     delivery_date_warning = fields.Boolean(compute='_compute_dlv_date_warn', store=False)
 | |
|     warn_vat_date = fields.Boolean(compute='_get_vat_warn_date', store=False, default=False)
 | |
|     date_vat_check = fields.Date(related='partner_invoice_id.date_vat_check')
 | |
|     vat = fields.Char(related='partner_invoice_id.vat')
 | |
| 
 | |
|     client_order_ref = fields.Char(track_visibility='onchange')
 | |
|     origin = fields.Char(track_visibility='onchange')
 | |
| 
 | |
|     partner_id = fields.Many2one(track_visibility='onchange')
 | |
|     partner_invoice_id = fields.Many2one(track_visibility='onchange')
 | |
|     partner_shipping_id = fields.Many2one(track_visibility='onchange')
 | |
| 
 | |
| #    pg9_call = fields.Char(string='PG9-Auftrag', compute='_pg9_call', store=False)
 | |
|     pg9_call_D = fields.Char(string='PG9-Auftrag_D', compute='_pg9_call', store=False)
 | |
|     pg9_call_T = fields.Char(string='PG9-Auftrag_T', compute='_pg9_call', store=False)
 | |
|     partner_flash = fields.Text(compute='_get_partner_flash', store=False)
 | |
|     pg9_complete = fields.Boolean(string='PG9-Komplett', compute='_compute_positions', store=False, default=False, copy=False)
 | |
| #    partner_flash = fields.Char(store=False)
 | |
| 
 | |
|     @api.multi
 | |
|     def _get_vat_warn_date(self):
 | |
|         for record in self:
 | |
|             if record.partner_invoice_id.date_vat_check:
 | |
|                 date_today = fields.Date.from_string(fields.Date.context_today(self))
 | |
|                 warn_date = date_today - relativedelta(months=+3)
 | |
|                 try:
 | |
|                     check_date = dateutil.parser.parse(record.date_vat_check).date()
 | |
|                 except:
 | |
|                     check_date = date_today - relativedelta(months=+36)
 | |
|                 if check_date < warn_date:
 | |
|                     record.warn_vat_date = True
 | |
|                 else:
 | |
|                     record.warn_vat_date = False
 | |
|             else:
 | |
|                 record.warn_vat_date = True
 | |
| 
 | |
|     @api.multi
 | |
|     def _compute_dlv_date_warn(self):
 | |
|         for record in self:
 | |
|             record.delivery_date_warning = False
 | |
|             if record.desired_delivery_date and record.delivery_date and record.desired_delivery_date > record.delivery_date:
 | |
|                 record.delivery_date_warning = True
 | |
| 
 | |
|     @api.multi
 | |
|     def _reset_sequence(self):
 | |
|         for rec in self:
 | |
|             current_sequence = 1
 | |
|             for line in rec.order_line:
 | |
|                 line.sequence = current_sequence
 | |
|                 current_sequence += 1
 | |
| 
 | |
|     @api.multi
 | |
|     def _pg9_call(self):
 | |
|         for record in self:
 | |
|             if record.order_type == 'D' and record.origin != False:
 | |
|                 xref = 'xref:' + record.origin
 | |
|                 record.pg9_call_D = 'http://localhost:9531/pg9/order/' + xref
 | |
| 
 | |
|             if record.order_type == 'T':
 | |
|                 xref = 'xref:' + record.name
 | |
|                 record.pg9_call_T = 'http://localhost:9531/pg9/order/' + xref
 | |
| 
 | |
|     # end def _pg9_call
 | |
| 
 | |
|     @api.multi
 | |
|     def _get_partner_flash(self):
 | |
|         for record in self:
 | |
|             record.partner_flash = record.partner_id.partner_flash
 | |
| 
 | |
|     @api.multi
 | |
|     @api.onchange('partner_invoice_id')
 | |
|     def _onchange_partner_invoice_id(self):
 | |
|         for record in self:
 | |
| #            record.partner_flash = record.partner_id.partner_flash
 | |
|             if not self.env.user.has_group('dp_custom.group_allow_third_country_sale'):
 | |
|                 if record.partner_invoice_id.country_id != record.partner_shipping_id.country_id:
 | |
|                     message = _('Rechnungs- und Lieferland passen nicht zusammen: %s <-> %s!') % \
 | |
|                                       (record.partner_invoice_id.country_id.name, record.partner_shipping_id.country_id.name)
 | |
| 
 | |
|                     raise Warning(_(message))
 | |
| 
 | |
|             if record.partner_invoice_id.is_retailer:
 | |
|                         record.payment_term_id = record.partner_invoice_id.property_payment_term_id
 | |
| 
 | |
|     @api.multi
 | |
|     @api.onchange('partner_shipping_id')
 | |
|     def onchange_partner_shipping_id(self):
 | |
|         res = super(SaleOrder, self).onchange_partner_shipping_id()
 | |
|         if not self.env.user.has_group('dp_custom.group_allow_third_country_sale'):
 | |
|             if self.partner_invoice_id.country_id != self.partner_shipping_id.country_id:
 | |
|                 message = _('Rechnungs- und Lieferland passen nicht zusammen: %s <-> %s!') % \
 | |
|                           (self.partner_invoice_id.country_id.name, self.partner_shipping_id.country_id.name)
 | |
| 
 | |
|                 raise Warning(_(message))
 | |
| 
 | |
|         self.carrier_id = self.partner_shipping_id.property_delivery_carrier_id
 | |
|         return {}
 | |
| 
 | |
|     @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
 | |
| #    @api.depends('assembly_state')
 | |
|     def _compute_positions(self):
 | |
|         for record in self:
 | |
|             pg_complete = True
 | |
|             p = 0
 | |
|             pp = 0
 | |
|             pa = 0
 | |
|             for line in record.order_line:
 | |
|                 p += 1
 | |
|                 if line.lot_id.assembled:
 | |
|                     pa += 1
 | |
|                 if line.product_id.material_type_id.production_relevant:
 | |
|                     pp += 1
 | |
|                     if line.product_uom_qty > line.qty_delivered and line.from_designbox:
 | |
|                         pg_complete = False
 | |
| 
 | |
|             record.pg9_complete = pg_complete
 | |
|             record.positions = p
 | |
|             record.production_positions = pp
 | |
|             record.assembly_positions = pa
 | |
| 
 | |
|     @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 picking.state != 'cancel' and (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')
 | |
|                 prefix = record.name[:4]
 | |
|                 newprefix = prefix.replace('OC', 'OF')
 | |
|                 record.quote_name = record.name.replace(prefix, newprefix)
 | |
|             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_update_confirmation(self, vals):
 | |
|         """
 | |
|         SST-3b
 | |
|         :param origin, confirmation_nr:
 | |
|         :return:
 | |
|         """
 | |
|         origin = vals.get('origin', False)
 | |
|         if origin:
 | |
|             order_id = self.search([('origin', '=', origin)], order='id DESC',limit=1)
 | |
|             if order_id and order_id.state != 'cancel':
 | |
|                 if order_id.state == 'sale' and (order_id.assembly_state == 'created'):
 | |
|                     vals['assembly_state'] = 'approved'
 | |
|                     vals['message_post'] = 'PG9 autoreleased'
 | |
|                 order_id.write(vals)
 | |
|                 if (order_id.state == 'draft' or order_id.state == 'sent') and (order_id.assembly_state == 'created'):
 | |
|                     order_id.action_confirm()
 | |
|                 return {'id': order_id.id, 'name': order_id.name}
 | |
|             else:
 | |
|                 cq = self.env['res.confirmation_queue'].search([('origin', '=', origin)])
 | |
|                 if cq:
 | |
|                     vals['confirmation_processed'] = False
 | |
|                     cq.write(vals)
 | |
|                 else:
 | |
|                     cq.create(vals)
 | |
|                 return {'id': -1}
 | |
| 
 | |
|     @api.model
 | |
|     def pg_update_quotation(self, vals):
 | |
|         """
 | |
|         SST-3a
 | |
|         :param order_name:
 | |
|         :return:
 | |
|         """
 | |
|         order_name = vals.get('order_name', False)
 | |
|         order_line_vals = vals.get('order_lines', False)
 | |
|         order_id = self.search([('name', '=', order_name)], order='id DESC',limit=1)
 | |
|         order_id.pg_create_order_lines(order_line_vals)
 | |
|         return {'id': order_id.id, 'name': order_id.name}
 | |
| 
 | |
|     @api.model
 | |
|     def pg_create_quotation(self, vals):
 | |
|         """
 | |
|         SST-3
 | |
|         :param vals:
 | |
|         :return:
 | |
|         """
 | |
| #        print('1')
 | |
|         if not vals.get('portal_id', False):
 | |
|             raise ValidationError(
 | |
|                 _("Der Kunde mit der Portal-ID \'%s\' kann nicht zugeordnet werden") % vals['portal_id'])
 | |
| 
 | |
|         portal_prefix = self.env.user.company_id.portal_prefix
 | |
|         if portal_prefix:
 | |
|             vals['portal_id'] = portal_prefix + '.' + 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
 | |
| #        print('2')
 | |
|         if not partner:
 | |
|             raise ValidationError(
 | |
|                 _("Der Kunde mit der Portal-ID \'%s\' kann nicht zugeordnet werden") % vals['portal_id'])
 | |
|         del vals['portal_id']
 | |
|         delivery_partner = self.env['res.partner']
 | |
|         delivery_vals = {}
 | |
| #        print('3')
 | |
|         if vals.get('portal_delivery_id', False):
 | |
|             if portal_prefix:
 | |
|                 vals['portal_delivery_id'] = portal_prefix + '.' + vals['portal_delivery_id']
 | |
| 
 | |
|             delivery_partner = self.env['res.partner'].search([('portal_id', '=', vals['portal_delivery_id'])])
 | |
|             delivery_vals['portal_id'] = vals['portal_delivery_id']
 | |
|             del vals['portal_delivery_id']
 | |
|         for key in list(vals.keys()):
 | |
|             if key.startswith('delivery_'):
 | |
|                 delivery_vals[key.replace('delivery_', '')] = vals[key]
 | |
|                 del vals[key]
 | |
| 
 | |
| #        print('4')
 | |
|         delivery_partner.with_context(delivery_partner=True).check_not_specified_fields(delivery_vals)
 | |
|         delivery_vals = delivery_partner.correct_values(delivery_vals)
 | |
|         delivery_vals['parent_id'] = partner.id
 | |
| 
 | |
|         if delivery_partner:
 | |
| #            print('4a')
 | |
|             allowed_update_keys = ['firstname', 'lastname', 'midname', 'email', 'phone']
 | |
|             for key in list(delivery_vals.keys()):
 | |
|                 if key not in allowed_update_keys:
 | |
|                     del delivery_vals[key]
 | |
| #            print('4a1')
 | |
|             delivery_partner.write(delivery_vals)
 | |
| #            print('4a2')
 | |
|         else:
 | |
| #            print('4b')
 | |
|             if not delivery_vals.get('type', False):
 | |
|                 delivery_vals['type'] = 'delivery'
 | |
|             delivery_vals['property_delivery_carrier_id'] = partner.property_delivery_carrier_id.id
 | |
|             delivery_partner = delivery_partner.create(delivery_vals)
 | |
| 
 | |
| #        print('5')
 | |
|         attachment_vals = vals.pop('attachment_ids', False)
 | |
|         order_line_vals = vals.get('order_lines', False)
 | |
| #        if 'attachment_ids' in vals: del vals['attachment_ids']
 | |
|         del vals['order_lines']
 | |
| #        print('6')
 | |
|         self.check_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
 | |
|         addr = partner.address_get(['delivery', 'invoice'])
 | |
| #        print('7')
 | |
|         vals['partner_invoice_id'] = vals.setdefault('partner_invoice_id', addr['invoice'])
 | |
|         partner_invoice_id = self.env['res.partner'].browse([vals['partner_invoice_id']])
 | |
|         fiscal_position_id = False
 | |
| #        print('8')
 | |
|         if partner_invoice_id:
 | |
|             if partner_invoice_id.property_account_position_id:
 | |
|                 fiscal_position_id = partner_invoice_id.property_account_position_id.id
 | |
|         dv = vals.get('desired_delivery_date',False)
 | |
|         if dv:
 | |
|             vals['desired_delivery_date'] = datetime.strptime(dv,'%d.%m.%Y').date()
 | |
| 
 | |
|         shipping_type = vals.get('shipping_type',False)
 | |
|         carrier_id = False
 | |
|         if shipping_type:
 | |
|             carrier_ids = self.env['delivery.carrier'].search([('key_mapping','=',shipping_type)])
 | |
|             if carrier_ids:
 | |
|                 carrier_id = carrier_ids[0].id
 | |
| 
 | |
|         if not carrier_id:
 | |
|             carrier_id = delivery_partner.property_delivery_carrier_id.id
 | |
| 
 | |
|         if not carrier_id:
 | |
|             carrier_id = partner.property_delivery_carrier_id.id
 | |
| 
 | |
|         confirmed = self.env['res.confirmation_queue'].search([('origin', '=', vals['origin']),('confirmation_processed','=',False)])
 | |
|         if confirmed:
 | |
|             vals['confirmation_nr'] = confirmed.confirmation_nr
 | |
|             vals.update({'confirm_order':True})
 | |
|             confirmed.confirmation_processed = True
 | |
|         vals.update({
 | |
|             'partner_id': partner.id,
 | |
|             'partner_invoice_id': partner_invoice_id.id,
 | |
|             'fiscal_position_id': fiscal_position_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': carrier_id
 | |
|         })
 | |
| #        print('9')
 | |
|         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)
 | |
|         if vals.get('confirm_order'):
 | |
|             order_id.action_confirm()
 | |
|         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'],
 | |
|                     'short_text': vals['lot_id']['short_text'],
 | |
|                     'assembled': vals['lot_id'].get('assembled',False),
 | |
|                     '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,
 | |
|                 'sequence': vals['sequence'],
 | |
|                 'name': vals['name'],
 | |
|                 'product_id': vals['product_id'],
 | |
|                 'price_unit': vals['price_unit'],
 | |
|                 'product_uom_qty': vals['product_uom_qty'],
 | |
|                 'lot_id': lot_id,
 | |
|                 'discount': vals.get('discount', 0),
 | |
|                 'hide_discount': vals.get('hide_discount', False),
 | |
|                 'from_designbox': vals.get('from_designbox',True),
 | |
|                 'customer_lead': vals.get('customer_lead', 0),
 | |
|             }))
 | |
|         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'])
 | |
| 
 | |
|         if vals.get('order_type', False):
 | |
|             selections = [selection[0] for selection in self._fields['order_type'].selection]
 | |
|             if vals.get('order_type') not in selections:
 | |
|                 raise ValidationError(_("Diese Auftragsart ist nicht bekannt!"))
 | |
|         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', 'shipping_type',
 | |
|                 'assembly_state', 'confirmation_nr', 'confirm_order', 'order_type', 'internal_notes', 'from_designbox', 'discount', 'hide_discount', 'desired_delivery_date', 'sequence']
 | |
| 
 | |
|     @api.model
 | |
|     def create(self,vals):
 | |
|         order = super(SaleOrder, self).create(vals)
 | |
|         current_sequence = 0
 | |
|         for order_line in order.order_line:
 | |
|             current_sequence += 1
 | |
|             order_line.update({'sequence': current_sequence,})
 | |
| 
 | |
|         # order.partner_id.last_order_changed = datetime.now()
 | |
| 
 | |
|         return order
 | |
| 
 | |
|     @api.multi
 | |
|     def write(self, vals):
 | |
|         if not self.editor_id.id and self.env.user.editor_name:
 | |
|             vals['editor_id'] = self.env.user.id
 | |
| 
 | |
|         if self.desired_delivery_date:
 | |
|             if not self.delivery_date:
 | |
|                 vals['delivery_date'] = self.desired_delivery_date
 | |
|             elif self.order_type == 'D':
 | |
|                 if not vals.get('delivery_date') or vals.get('delivery_date','') == '':
 | |
|                     vals['delivery_date'] = self.delivery_date
 | |
| 
 | |
|         res = super(SaleOrder, self).write(vals)
 | |
| 
 | |
|         # if self.partner_invoice_id.country_id != self.partner_shipping_id.country_id:
 | |
|         #     message = _('Rechnungs- und Lieferland passen nicht zusammen: %s <-> %s!') % \
 | |
|         #               (self.partner_invoice_id.country_id, self.partner_shipping_id.country_id)
 | |
|         #
 | |
|         #     raise Warning(_(message))
 | |
|         #
 | |
| 
 | |
|         if vals.get('assembly_state', False) == 'done':
 | |
|             self.message_post(body='Produktion fertig')
 | |
| 
 | |
|         if vals.get('message_post',False):
 | |
|             self.message_post(body=vals.get('message_post'))
 | |
| 
 | |
|         if vals.get('order_line',False):
 | |
|             self._reset_sequence()
 | |
| 
 | |
|         # if self.partner_id:
 | |
|         #     self.partner_id.last_order_changed = datetime.now()
 | |
|         #
 | |
|         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 = {}
 | |
|         seq = 0
 | |
|         for order in self.sorted(key=lambda o: o.name):
 | |
|             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.sequence, l.qty_to_invoice < 0)):
 | |
|                 if float_is_zero(line.qty_to_invoice, precision_digits=precision):
 | |
|                     continue
 | |
|                 seq += 1
 | |
|                 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 == "siehe Detail" or order.name not in invoices[group_key].origin.split(', '):
 | |
|                         vals['origin'] = "siehe Detail"
 | |
|                     if order.client_order_ref and (order.client_order_ref == "siehe Detail" or (
 | |
|                             order.client_order_ref not in invoices[group_key].name.split(
 | |
|                         ', ') and order.client_order_ref != invoices[group_key].name)):
 | |
|                         vals['name'] = "siehe Detail"
 | |
|                     invoices[group_key].write(vals)
 | |
|                 if line.qty_to_invoice > 0:
 | |
|                     line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice,seq)
 | |
|                 elif line.qty_to_invoice < 0 and final:
 | |
|                     line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice,seq)
 | |
| 
 | |
|             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_vals = {}
 | |
|         if self.name[2:4] == 'OF':
 | |
| #            new_name = re.sub(r"^ATOF", "ATOC", self.name)
 | |
|             new_name = ''.join((self.name[:2],'OC',self.name[4:]))
 | |
| #            self.name = new_name
 | |
|             new_vals['name'] = new_name
 | |
| 
 | |
|         pg_release_mode = self.env['ir.config_parameter'].get_param('pg_release_mode','MANUAL')
 | |
|         if (self.confirmation_nr and self.confirmation_nr != '') and self.order_type == 'D' \
 | |
|                 and self.assembly_state == 'created' and pg_release_mode == 'auto':
 | |
| #            self.assembly_state = 'approved'
 | |
|             new_vals['assembly_state'] = 'approved'
 | |
|             new_vals['message_post'] = 'PG9 autoreleased'
 | |
| 
 | |
|         if self.editor_id == '' and self.env.user.editor_name:
 | |
|             self.editor_id = self.env.user.editor_name
 | |
| 
 | |
|         self.write(new_vals)
 | |
|         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
 | |
| 
 | |
|     @api.multi
 | |
|     @api.onchange('partner_id')
 | |
|     def onchange_partner_id(self):
 | |
|         # for record in self:
 | |
|         #     record.partner_flash = record.partner_id.partner_flash
 | |
| 
 | |
|         if not self.partner_id:
 | |
|             self.update({
 | |
|                 'partner_invoice_id': False,
 | |
|                 'partner_shipping_id': False,
 | |
|             })
 | |
|             return
 | |
| 
 | |
|         addr = self.partner_id.address_get(['delivery', 'invoice'])
 | |
|         # values = {
 | |
|         #     'partner_invoice_id': addr['invoice'],
 | |
|         #     'partner_shipping_id': addr['delivery'],
 | |
|         #     'pricelist_id': self.partner_id.property_product_pricelist and self.partner_id.property_product_pricelist.id or False,
 | |
|         #     'user_id': self.partner_id.user_id.id or self.env.uid
 | |
|         # }
 | |
|         values = {
 | |
|             'partner_invoice_id': addr['invoice'],
 | |
|             'partner_shipping_id': addr['delivery'],
 | |
|             'pricelist_id': self.partner_id.property_product_pricelist and self.partner_id.property_product_pricelist.id or False,
 | |
|             'user_id': self.partner_id.user_id.id
 | |
|         }
 | |
|         if self.env['ir.config_parameter'].sudo().get_param(
 | |
|                 'sale.use_sale_note') and self.env.user.company_id.sale_note:
 | |
|             values['note'] = self.with_context(lang=self.partner_id.lang).env.user.company_id.sale_note
 | |
| 
 | |
|         if self.partner_id.team_id:
 | |
|             values['team_id'] = self.partner_id.team_id.id
 | |
| 
 | |
|         if self.partner_id:
 | |
|             values['partner_flash'] = self.partner_id.partner_flash
 | |
| 
 | |
|         self.update(values)
 | |
| 
 | |
|     @api.multi
 | |
|     @api.onchange('partner_invoice_id')
 | |
|     def onchange_partner_invoice_id(self):
 | |
|         if not self.partner_invoice_id:
 | |
|             self.update({
 | |
|                 'payment_term_id': False,
 | |
|                 'fiscal_position_id': False,
 | |
|             })
 | |
|             return
 | |
| 
 | |
|         values = {
 | |
|             'fiscal_position_id': self.partner_invoice_id.property_account_position_id and self.partner_invoice_id.property_account_position_id.id or False,
 | |
|             'payment_term_id': self.partner_invoice_id.property_payment_term_id and self.partner_invoice_id.property_payment_term_id.id or False,
 | |
|         }
 | |
|         self.update(values)
 | |
| 
 | |
| 
 | |
| class SaleOrderLine(models.Model):
 | |
|     _inherit = 'sale.order.line'
 | |
| 
 | |
|     @api.model
 | |
|     def _get_product_id_domain(self):
 | |
|         return [('sale_ok', '=', True), '|', ('can_be_sold_unconfigured', '=', True), ('type', '=', 'service')]
 | |
| 
 | |
|     lot_id = fields.Many2one(comodel_name='stock.production.lot', string='Lot')
 | |
|     from_designbox = fields.Boolean(string='I', readonly=True, help='Import von externem System')
 | |
|     product_id = fields.Many2one(domain=_get_product_id_domain)
 | |
|     weight = fields.Float(string='Gewicht', compute='_compute_weight')
 | |
|     intrastat_id = fields.Many2one(comodel_name='report.intrastat.code', string='Intrastat Code')
 | |
|     sequence = fields.Integer(string='Sequence', default=9999)
 | |
|     item_notes = fields.Text(string='Notes', related='lot_id.notes', store=False)
 | |
|     item_warn = fields.Boolean(string='Notes!!!', compute='_compute_item_warn', store=False)
 | |
|     price_change = fields.Boolean(string='R', help='Preis aus Artikelstamm auslesen',default=False, store=False, track_visibility='always')
 | |
|     read_price = fields.Boolean(string='R',default=False, store=False, help='Preis aus Artikelstamm auslesen', track_visibility='always')
 | |
| 
 | |
|     @api.multi
 | |
|     def _compute_item_warn(self):
 | |
|         for record in self:
 | |
|             iNote = str(record.lot_id.notes)
 | |
|             record.item_warn = True if iNote.startswith('!') else False
 | |
| 
 | |
|     @api.multi
 | |
|     @api.onchange('product_id')
 | |
|     def product_id_change(self):
 | |
|         result = super(SaleOrderLine,self).product_id_change()
 | |
|         product = self.product_id.with_context(
 | |
|             lang=self.order_id.partner_id.lang,
 | |
|             partner=self.order_id.partner_id.id
 | |
|         )
 | |
| 
 | |
|         name = product.name
 | |
|         if product.description_sale:
 | |
|             name = product.description_sale
 | |
|         self.name = name
 | |
| 
 | |
|         return result
 | |
| 
 | |
|     @api.onchange('product_uom_qty','read_price')
 | |
|     def product_uom_change(self):
 | |
|         save_price_unit = self.price_unit
 | |
|         result = super(SaleOrderLine,self).product_uom_change()
 | |
|         if save_price_unit != self.price_unit:
 | |
|             self.price_change = True
 | |
|         if not self.read_price:
 | |
|             self.price_unit = save_price_unit
 | |
|         else:
 | |
|             self.price_change = False
 | |
|         self.read_price = False
 | |
| 
 | |
|     @api.model
 | |
|     def create(self, vals):
 | |
|         if not vals.get('intrastat_id', False):
 | |
|             if vals.get('lot_id', False):
 | |
|                 vals.update(intrastat_id=self.env['stock.production.lot'].browse(vals['lot_id']).intrastat_id.id)
 | |
|             elif vals.get('product_id', False):
 | |
|                 vals.update(intrastat_id=self.env['product.product'].browse(vals['product_id']).intrastat_id.id)
 | |
|         return super(SaleOrderLine, self).create(vals)
 | |
| 
 | |
|     @api.multi
 | |
|     def write(self, vals):
 | |
|         for record in self:
 | |
|             lot_id = record.lot_id if 'lot_id' not in vals else vals.get('lot_id', False)
 | |
|             if vals.get('intrastat_id', False) and lot_id:
 | |
|                 self.env['stock.production.lot'].browse([lot_id.id]).write({
 | |
|                     'intrastat_id': vals.get('intrastat_id')
 | |
|                 })
 | |
|             # elif vals.get('intrastat_id', False) and not lot_id:
 | |
|             #     raise UserError(_('Der Intrastrat Code kann nur gesetzt werden wenn ein Lot angegeben wurde.'))
 | |
| 
 | |
|             if record.product_id.type == 'service' and self.env.user.has_group('dp_custom.group_allow_change_pg_status'):
 | |
|                 not_allowed_designbox_keys = ['product_uom', 'price_unit']
 | |
|                 if not self.check_allowed_vals_from_designbox(not_allowed_designbox_keys, vals):
 | |
|                     raise ValidationError(
 | |
|                         _("Preise können von Dienstleistungen aus der Designbox nicht geändert werden"))
 | |
|             else:
 | |
|                 not_allowed_designbox_keys = ['product_uom_qty', 'product_uom', 'price_unit']
 | |
|                 if not self.check_allowed_vals_from_designbox(not_allowed_designbox_keys, vals):
 | |
|                     raise ValidationError(_("Menge und Preis können von Produkten aus der Designbox nicht geändert werden"))
 | |
|         return super(SaleOrderLine, self).write(vals)
 | |
| 
 | |
|     @api.multi
 | |
|     def check_allowed_vals_from_designbox(self, not_allowed_designbox_keys, vals):
 | |
|         self.ensure_one()
 | |
|         allowed_write = True
 | |
|         precision_digits = self.env['decimal.precision'].precision_get('Product Price')
 | |
|         if (self.from_designbox and self.product_id.default_code!='TZ_PLATTE') and set(vals.keys()).intersection(not_allowed_designbox_keys):
 | |
|             for val_key in not_allowed_designbox_keys:
 | |
|                 if val_key in list(vals.keys()) and type(self.__getattribute__(val_key)) is float:
 | |
|                     if float_compare(self.__getattribute__(val_key), vals[val_key],
 | |
|                                      precision_digits=precision_digits) != 0:
 | |
|                         allowed_write = False
 | |
|                         break
 | |
|                 elif val_key in list(vals.keys()) and type(self.__getattribute__(val_key)) is type(self.env['product.uom']):
 | |
|                     if self.__getattribute__(val_key).id != vals[val_key]:
 | |
|                         allowed_write = False
 | |
|                         break
 | |
|                 elif val_key in list(vals.keys()):
 | |
|                     allowed_write = False
 | |
|                     break
 | |
|         return allowed_write
 | |
| 
 | |
|     # @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
 | |
|     #             delay = product_id.product_tmpl_id.sale_delay
 | |
|     #             if vals.get('delivery_date',False):
 | |
|     #                 dlvd = datetime.strptime(vals.get('delivery_date'),'%d.%m.%Y').date()
 | |
|     #                 dlvdiff = (dlvd - datetime.now().date()).days
 | |
|     #                 if dlvdiff  > 0 and dlvdiff > product_id.product_tmpl_id.sale_delay:
 | |
|     #                     delay = dlvdiff
 | |
|     #             vals['customer_lead'] = delay
 | |
|     #         else:
 | |
|     #             raise ValidationError(
 | |
|     #                 _("Produkt \'%s\' kann nicht zugeordnet werden") % vals['product_id'])
 | |
|     #     return 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
 | |
|                 delay = product_id.product_tmpl_id.sale_delay
 | |
|                 # if vals.get('delivery_date',False):
 | |
|                 #     dlvd = datetime.strptime(vals.get('delivery_date'),'%d.%m.%Y').date()
 | |
|                 #     dlvdiff = (dlvd - datetime.now().date()).days
 | |
|                 #     if dlvdiff  > 0 and dlvdiff > product_id.product_tmpl_id.sale_delay:
 | |
|                 #         delay = dlvdiff
 | |
|                 vals['customer_lead'] = delay
 | |
|             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 _prepare_invoice_line(self, qty):
 | |
|         self.ensure_one()
 | |
|         res = super(SaleOrderLine, self)._prepare_invoice_line(qty)
 | |
| 
 | |
|         res['lot_id'] = self.lot_id.id
 | |
|         res['intrastat_id'] = self.intrastat_id.id
 | |
|         return res
 | |
| 
 | |
|     @api.multi
 | |
|     def invoice_line_create(self, invoice_id, qty, seq):
 | |
|         """
 | |
|         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, 'sequence' : seq, 'sale_line_ids': [(6, 0, [line.id])]})
 | |
|                 invoice_lines |= self.env['account.invoice.line'].create(vals)
 | |
|                 if line.order_id.partner_invoice_id.is_retailer:
 | |
|                     discount = line.calc_discount()
 | |
|                     discount = int(round(discount))
 | |
|                     if discount > 0:
 | |
|                         vals.update({
 | |
|                             'price_unit': -invoice_lines.price_subtotal * (discount / 100),
 | |
|                             'quantity': 1.0,
 | |
|                             'uom_id': self.env.ref('product.product_uom_unit').id,
 | |
|                             'name': 'Händlerrabatt {}%'.format(discount),
 | |
|                             'hide_intrastat_code': True,
 | |
|                             'dealer_discount': True
 | |
|                         })
 | |
|                         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).sudo()._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
 |