Add check to create warehouse for a new party.

This commit is contained in:
Sergio Morillo 2021-06-30 16:55:26 +02:00
parent 4de14a97dd
commit ebe45e5f2a
6 changed files with 68 additions and 152 deletions

View File

@ -8,8 +8,12 @@ msgid ""
msgstr "\"Desde ubicación\" o \"A ubicación\" del albarán interno \"%s\" debe ser una ubicación del almacén de tercero \"%s\"."
msgctxt "field:party.party,warehouse:"
msgid "Warehouse"
msgstr "Almacén"
msgid "Party Warehouse"
msgstr "Almacén de tercero"
msgctxt "field:party.party,create_warehouse:"
msgid "Create Warehouse"
msgstr "Crear almacén"
msgctxt "field:party.party.create_warehouse.start,id:"
msgid "ID"

View File

@ -10,10 +10,44 @@ from trytond.transaction import Transaction
class Party(metaclass=PoolMeta):
__name__ = 'party.party'
warehouse = fields.Many2One('stock.location', 'Warehouse',
warehouse = fields.Many2One('stock.location', 'Party Warehouse',
domain=[
('type', '=', 'warehouse'),
])
create_warehouse = fields.Function(
fields.Boolean('Create Warehouse',
states={
'readonly': Bool(Eval('warehouse')) | ~Eval('active'),
'invisible': Bool(Eval('warehouse'))
},
depends=['warehouse', 'active']),
'get_create_warehouse', setter='set_create_warehouse')
def get_create_warehouse(self, name=None):
return bool(self.warehouse)
@classmethod
def set_create_warehouse(cls, records, name, value):
pool = Pool()
CreateWarehouse = pool.get('party.party.create_warehouse',
type='wizard')
Configuration = pool.get('stock.configuration')
Location = pool.get('stock.location')
if not value:
return
to_save = []
conf = Configuration(1)
for record in records:
if not record.warehouse:
warehouse = CreateWarehouse._get_warehouse(Location,
record.name, conf.party_warehouse_parent)
warehouse.save()
record.warehouse = warehouse
to_save.append(record)
if to_save:
cls.save(to_save)
class CreateWarehouseStart(ModelView):
@ -128,7 +162,8 @@ class CreateWarehouse(Wizard):
Party.write(parties, {'warehouse': warehouse})
return 'end'
def _get_warehouse(self, Location, name, parent):
@classmethod
def _get_warehouse(cls, Location, name, parent):
ModelData = Pool().get('ir.model.data')
prod_location = Location(ModelData.get_id(

View File

@ -1,148 +0,0 @@
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
import argparse
import datetime
def parse_commandline():
options = {}
parser = argparse.ArgumentParser(prog='migrate',
description='Migrate locations')
parser.add_argument("-c", "--config", dest="configfile", metavar='FILE',
default=None, help="specify config file")
parser.add_argument("-d", "--database", dest="database_name", default=None,
metavar='DATABASE', help="specify the database name")
options = parser.parse_args()
if not options.database_name:
parser.error('Missing database option')
return options
def create_party_warehouses():
pool = Pool()
ModelData = pool.get('ir.model.data')
Party = pool.get('party.party')
PartyLocation = pool.get('party.party.location')
Location = pool.get('stock.location')
ProductLimit = pool.get('stock.location.product_limit')
Inventory = pool.get('stock.inventory')
Configuration = pool.get('stock.configuration')
conf = Configuration(1)
customer_loc = Location(ModelData.get_id('stock', 'location_customer'))
production_loc = Location(ModelData.get_id(
'production', 'location_production'))
parties = [p.party for p in PartyLocation.search([
('customer_location', '!=', None),
('customer_location', '!=', customer_loc),
])]
parties = sorted(parties, key=lambda p: p.customer_location)
to_save = []
to_save_inventories = []
to_write_parties = []
to_write_products_limits = []
for location, grouped_parties in groupby(parties,
key=lambda p: p.customer_location):
grouped_parties = list(grouped_parties)
party_location = Location(
name=location.name,
type='storage')
party_location.save()
warehouse = Location(
name=location.name,
type='warehouse',
parent=conf.party_warehouse_parent,
input_location=party_location,
output_location=party_location,
storage_location=party_location,
production_location=production_loc,
)
to_save.append(warehouse)
to_write_parties.extend((grouped_parties, {
'warehouse': warehouse,
'customer_location': customer_loc,
}))
# Modify location on products limits
pls = ProductLimit.search([('location', '=', location)])
if pls:
to_write_products_limits.extend((pls, {
'location': warehouse,
}))
# Create inventories
inventory = get_inventory(location, party_location)
if inventory:
to_save_inventories.append(inventory)
if to_save:
Location.save(to_save)
print('\033[0;32m', 'Warehouses created successfully.', '\033[0;m')
if to_write_parties:
Party.write(*to_write_parties)
print('\033[0;32m', 'Parties modified successfully.', '\033[0;m')
if to_write_products_limits:
ProductLimit.write(*to_write_products_limits)
print('\033[0;32m', 'Products limits modified successfully.',
'\033[0;m')
if to_save_inventories:
Inventory.save(to_save_inventories)
Inventory.confirm(to_save_inventories)
print('\033[0;32m', 'Invetories created successfully.', '\033[0;m')
def get_inventory(location, party_location):
pool = Pool()
Inventory = pool.get('stock.inventory')
InventoryLine = pool.get('stock.inventory.line')
Location = pool.get('stock.location')
ModelData = pool.get('ir.model.data')
Product = pool.get('product.product')
with Transaction().set_context(locations=[location.id],
stock_date_end=datetime.date.today()):
products = Product.search([
('type', '=', 'goods'),
('consumable', '=', False),
('quantity', '>', 0.0),
])
if not products:
return
lines = [InventoryLine(
product=p.id,
quantity=p.quantity,
) for p in products]
lost_found_loc = Location(ModelData.get_id(
'stock', 'location_lost_found'))
inventory = Inventory(
location=party_location,
date=datetime.date.today(),
lost_found=lost_found_loc,
company=1,
state='draft',
lines=lines,
)
return inventory
if __name__ == '__main__':
options = parse_commandline()
from trytond.transaction import Transaction
from trytond.pool import Pool
from itertools import groupby
from trytond.config import config as CONFIG
CONFIG.update_etc(options.configfile)
Pool.start()
pool = Pool(options.database_name)
pool.init()
with Transaction().start(options.database_name, 0, context={}):
print('Creating parties warehouses...')
create_party_warehouses()

View File

@ -6,6 +6,11 @@ from trytond.pool import PoolMeta
class Location(metaclass=PoolMeta):
__name__ = 'stock.location'
@classmethod
def __setup__(cls):
super().__setup__()
cls.parent.states.pop('invisible')
@classmethod
def _parent_domain(cls):
# TODO: Remove parent field in 6.2 version

View File

@ -58,6 +58,8 @@ Create parties::
>>> party4.save()
>>> party5 = Party(name='Party 5')
>>> party5.save()
>>> bool(party5.create_warehouse)
False
Add parties warehouses::
@ -104,6 +106,22 @@ Add parties warehouses::
>>> party5.reload()
>>> party4.warehouse == party5.warehouse
True
>>> bool(party5.create_warehouse)
True
Add party with create_warehoues check::
>>> party6 = Party(name='Party 6')
>>> party6.create_warehouse = True
>>> party6.save()
>>> bool(party6.warehouse)
True
>>> party6.warehouse.name == party6.name
True
>>> party6.warehouse.parent == view_loc
True
>>> party6.warehouse.storage_location == party6.warehouse.input_location == party6.warehouse.output_location
True
Create product::

View File

@ -5,5 +5,7 @@
<xpath expr="/form/notebook/page[@id='stock']/field[@name='supplier_location']" position="after">
<label name="warehouse"/>
<field name="warehouse"/>
<label name="create_warehouse"/>
<field name="create_warehouse"/>
</xpath>
</data>