Skip to content

Commit

Permalink
[REF] Chiusura issue OCA#71 OCA#30 OCA#72 OCA#73 OCA#80
Browse files Browse the repository at this point in the history
  • Loading branch information
Borruso committed Nov 20, 2021
1 parent c366105 commit 4f09f4c
Show file tree
Hide file tree
Showing 14 changed files with 631 additions and 354 deletions.
42 changes: 33 additions & 9 deletions l10n_it_delivery_note/cli/migrate_l10n_it_ddt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


# noinspection PyPep8Naming
class MigrateL10nItDdt(EasyCommand):
class Migrate_L10n_It_Ddt(EasyCommand):
_carriage_conditions = None
_goods_descriptions = None
_transportation_reasons = None
Expand Down Expand Up @@ -87,14 +87,15 @@ def check_database_integrity(self):
))

old_sequence = self.env.ref('l10n_it_ddt.seq_ddt')
if old_sequence.number_next_actual == 1:
old_sequence_max = max(old_sequence.mapped('date_range_ids.number_next_actual'))
if old_sequence_max == 1:
raise UserError(_(
"It seems that there are no documents to migrate. "
"You don't need to run this command."
))

new_sequence = self.env.ref(
'l10n_it_delivery_note_base.delivery_note_sequence_ddt')
new_sequence = self.env.ref('l10n_it_delivery_note_base.'
'delivery_note_sequence_ddt')
if new_sequence.number_next_actual > 1:
raise ValidationError(_(
"It seems that at least one delivery note has been "
Expand Down Expand Up @@ -234,16 +235,20 @@ def migrate_document_types(self):
'sequence_id': r.sequence_id.id,
'default_goods_appearance_id':
self._goods_descriptions[
r.default_goods_description_id].id,
r.default_goods_description_id].id
if r.default_goods_description_id else None,
'default_transport_reason_id':
self._transportation_reasons[
r.default_transportation_reason_id].id,
r.default_transportation_reason_id].id
if r.default_transportation_reason_id else None,
'default_transport_condition_id':
self._carriage_conditions[
r.default_carriage_condition_id].id,
r.default_carriage_condition_id].id
if r.default_carriage_condition_id else None,
'default_transport_method_id':
self._transportation_methods[
r.default_transportation_method_id].id,
r.default_transportation_method_id].id
if r.default_transportation_method_id else None,
'note': r.note
})

Expand Down Expand Up @@ -296,7 +301,26 @@ def vals_getter(record):

documents = Document.search([], order='id ASC')
for document in documents:
DeliveryNote.create(vals_getter(document))
delivery_note = DeliveryNote.create(vals_getter(document))
extra_lines = document.line_ids.filtered(lambda l: not l.move_id)

if extra_lines:
lines_vals = []

for line in extra_lines:
lines_vals.append({
'name': line.name,
'product_id': line.product_id.id,
'product_qty': line.product_uom_qty,
'product_uom_id': line.product_uom_id.id,
'price_unit': line.price_unit,
'discount': line.discount,
'tax_ids': [(4, t.id) for t in line.tax_ids]
})

delivery_note.write({
'line_ids': [(0, False, vals) for vals in lines_vals]
})

_logger.info("Documents data successfully migrated.")

Expand Down
2 changes: 1 addition & 1 deletion l10n_it_delivery_note/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from . import account_invoice
from . import res_company
from . import res_config_settings
from . import res_partner
from . import sale_order
from . import stock_delivery_note
from . import stock_location
from . import stock_picking
40 changes: 32 additions & 8 deletions l10n_it_delivery_note/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,41 @@ def update_delivery_note_lines(self):
#
context['lang'] = invoice.partner_id.lang

for note in invoice.delivery_note_ids:
new_lines.append((0, False, {
'sequence': 99, 'display_type': 'line_note',
'name':
_("""Delivery Note "{}" of {}""").
format(note.name, note.date.strftime(DATE_FORMAT)),
'delivery_note_id': note.id
}))
for line in invoice.invoice_line_ids:
for sale in line.sale_line_ids:
delivery_note_line = \
invoice.delivery_note_ids.mapped('line_ids') \
& sale.delivery_note_line_ids
delivery_note = delivery_note_line.mapped('delivery_note_id')
for note in delivery_note:
new_lines.append((0, False, {
'sequence': line.sequence - 1,
'display_type': 'line_note',
'name':
_("""Delivery Note "{}" of {}""").format(
note.name,
note.date.strftime(DATE_FORMAT)
),
'delivery_note_id': note.id
}))

invoice.write({'invoice_line_ids': new_lines})

@api.multi
def unlink(self):
# Ripristino il valore delle delivery note
# per poterle rifatturare
inv_lines = self.mapped('invoice_line_ids')
all_dnls = inv_lines.mapped('sale_line_ids').mapped('delivery_note_line_ids')
inv_dnls = self.mapped('delivery_note_ids').mapped('line_ids')
dnls_to_unlink = all_dnls & inv_dnls
res = super().unlink()
dnls_to_unlink.sync_invoice_status()
dnls_to_unlink.mapped('delivery_note_id')._compute_invoice_status()
for dn in dnls_to_unlink.mapped('delivery_note_id'):
dn.state = 'confirm'
return res


class AccountInvoiceLine(models.Model):
_inherit = 'account.invoice.line'
Expand Down
8 changes: 0 additions & 8 deletions l10n_it_delivery_note/models/res_company.py

This file was deleted.

12 changes: 9 additions & 3 deletions l10n_it_delivery_note/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

def _default_virtual_locations_root(self):
return self.env.ref('stock.stock_location_locations_virtual',
raise_if_not_found=False)

group_use_advanced_delivery_notes = fields.Boolean(
string="Use Advanced DN Features",
implied_group='l10n_it_delivery_note.'
'use_advanced_delivery_notes')

draft_delivery_note_invoicing_notify = fields.Boolean(
related='company_id.draft_delivery_note_invoicing_notify',
readonly=False)
virtual_locations_root = fields.Many2one('stock.location',
string="Virtual locations root",
default=_default_virtual_locations_root,
config_parameter='stock.location'
'.virtual_root')
103 changes: 74 additions & 29 deletions l10n_it_delivery_note/models/stock_delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from odoo.addons import decimal_precision as dp
from odoo.exceptions import UserError

from ..mixins.picking_checker import DONE_PICKING_STATE, PICKING_TYPES
from ..mixins.picking_checker import \
DONE_PICKING_STATE, PICKING_TYPES, DOMAIN_PICKING_TYPES

DATE_FORMAT = '%d/%m/%Y'
DATETIME_FORMAT = '%d/%m/%Y %H:%M:%S'
Expand Down Expand Up @@ -53,7 +54,8 @@ def _default_company(self):
return self.env.user.company_id

def _default_type(self):
return self.env['stock.delivery.note.type'].search([], limit=1)
return self.env['stock.delivery.note.type'] \
.search([('code', '=', DOMAIN_PICKING_TYPES[1])], limit=1)

def _default_volume_uom(self):
return self.env.ref('uom.product_uom_litre', raise_if_not_found=False)
Expand Down Expand Up @@ -219,32 +221,19 @@ def _domain_weight_uom(self):
default=_default_company)

@api.multi
@api.depends('name', 'partner_id',
'partner_ref', 'partner_id.display_name')
@api.depends('name', 'partner_ref',
'partner_id', 'partner_id.display_name')
def _compute_display_name(self):
for note in self:
if not note.name:
partner_name = note.partner_id.display_name
create_date = note.create_date.strftime(DATETIME_FORMAT)
name = "{} - {}".format(partner_name, create_date)

else:
name = note.name

if note.partner_ref and note.type_code == 'incoming':
name = "{} ({})".format(name, note.partner_ref)

note.display_name = name
return super()._compute_display_name()

@api.multi
@api.depends('state', 'line_ids', 'line_ids.invoice_status')
def _compute_invoice_status(self):
for note in self:
lines = note.line_ids.filtered(lambda l: l.sale_line_id)

if lines and all(
line.invoice_status == DOMAIN_INVOICE_STATUSES[2]
for line in lines):
if all(line.invoice_status == DOMAIN_INVOICE_STATUSES[2]
for line in lines):
note.state = DOMAIN_DELIVERY_NOTE_STATES[2]
note.invoice_status = DOMAIN_INVOICE_STATUSES[2]

Expand Down Expand Up @@ -313,13 +302,27 @@ def _compute_boolean_flags(self):
(note.state == 'draft' and can_change_number)
note.show_product_information = show_product_information

@api.onchange('picking_type')
def _onchange_picking_type(self):
if self.picking_type:
type_domain = [('code', '=', self.picking_type)]

else:
type_domain = []

return {'domain': {'type_id': type_domain}}

@api.onchange('type_id')
def _onchange_type(self):
if self.type_id:
if self.name and self.type_id.sequence_id != self.sequence_id:
raise UserError(_("You cannot set this delivery note type due"
" of a different numerator configuration."))

if self.picking_type and self.type_id.code != self.picking_type:
raise UserError(_("You cannot set this delivery note type due"
" of a different type with related pickings."))

if self._update_generic_shipping_information(self.type_id):
return {
'warning': {
Expand Down Expand Up @@ -372,6 +375,25 @@ def _onchange_partner_shipping(self):
else:
self.delivery_method_id = False

@api.multi
def name_get(self):
result = []

for note in self:
name = note.name

if not note.name:
partner_name = note.partner_id.display_name
create_date = note.create_date.strftime(DATETIME_FORMAT)
name = "{} - {}".format(partner_name, create_date)

elif note.partner_ref and note.type_code == 'incoming':
name = "{} ({})".format(name, note.partner_ref)

result.append((note.id, name))

return result

def check_compliance(self, pickings):
super().check_compliance(pickings)

Expand Down Expand Up @@ -416,7 +438,7 @@ def _fix_quantities_to_invoice(self, lines):
move_ids = line.move_ids & pickings_move_ids
qty_to_invoice = sum(move_ids.mapped('quantity_done'))

if qty_to_invoice < (line.product_uom_qty - line.qty_to_invoice):
if qty_to_invoice < line.qty_to_invoice:
cache[line] = line.fix_qty_to_invoice(qty_to_invoice)

return cache
Expand All @@ -441,7 +463,7 @@ def action_invoice(self):
if order_lines.filtered(lambda l: l.need_to_be_invoiced):
cache[downpayment] = downpayment.fix_qty_to_invoice()

self.sale_ids \
invoice_ids = self.sale_ids \
.filtered(lambda o: o.invoice_status == DOMAIN_INVOICE_STATUSES[1]) \
.action_invoice_create(final=True)

Expand All @@ -450,6 +472,18 @@ def action_invoice(self):

orders_lines._get_to_invoice_qty()

for line in self.line_ids:
line.write({
'invoice_status': 'invoiced'
})
if all(line.invoice_status == 'invoiced' for line in self.line_ids):
self.write({
'invoice_ids': [(4, invoice_id) for invoice_id in invoice_ids]
})
self._compute_invoice_status()
invoices = self.env['account.invoice'].browse(invoice_ids)
invoices.update_delivery_note_lines()

@api.multi
def action_done(self):
self.write({'state': DOMAIN_DELIVERY_NOTE_STATES[3]})
Expand Down Expand Up @@ -642,23 +676,34 @@ def is_invoiceable(self):
@api.onchange('product_id')
def _onchange_product_id(self):
if self.product_id:
domain = [
('category_id', '=', self.product_id.uom_id.category_id.id)]
self.name = \
self.product_id.get_product_multiline_description_sale()

name = self.product_id.name
if self.product_id.description_sale:
name += '\n' + self.product_id.description_sale

self.name = name

product_uom_domain = [
('category_id', '=', self.product_id.uom_id.category_id.id)
]

else:
domain = []
product_uom_domain = []

return {'domain': {'product_uom_id': domain}}
return {'domain': {'product_uom_id': product_uom_domain}}

@api.model
def _prepare_detail_lines(self, moves):
lines = []
for move in moves:

name = move.product_id.name
if move.product_id.description_sale:
name += '\n' + move.product_id.description_sale

line = {
'move_id': move.id,
'name': move.name,
'name': name,
'product_id': move.product_id.id,
'product_qty': move.product_uom_qty,
'product_uom_id': move.product_uom.id
Expand Down
Loading

0 comments on commit 4f09f4c

Please sign in to comment.