Add products in inventory entries

This commit is contained in:
Mauricio Baeza 2021-06-16 23:36:25 -05:00
parent 9be913c47d
commit 3514944f5d
2 changed files with 31 additions and 16 deletions

View File

@ -1457,6 +1457,13 @@ class Categorias(BaseModel):
except:
return None
@classmethod
def _get_by_name(cls, name):
try:
return Categorias.get(Categorias.categoria==name)
except:
return None
@classmethod
def get_all(cls):
rows = (Categorias.select(
@ -9469,41 +9476,47 @@ class InventoryEntries(BaseModel):
return obj, msg
# ~ teresa
def _get_or_create_product(self, args):
def _get_or_create_product(self, args, cant):
# ~ almacen = ForeignKeyField(Almacenes, null=True)
# ~ categoria = ForeignKeyField(Categorias, null=True)
# ~ clave = TextField(unique=True, index=True)
# ~ unidad = ForeignKeyField(SATUnidades)
# ~ ultimo_costo = DecimalField(default=0.0, max_digits=18, decimal_places=6,
# ~ auto_round=True)
# ~ existencia = DecimalField(default=0.0, max_digits=18, decimal_places=2,
# ~ auto_round=True)
# ~ impuestos = ManyToManyField(SATImpuestos, related_name='productos')
id = int(args.get('id_product', '0'))
if id:
obj = Productos.get(Productos.id==id)
else:
# ~ {'cant': '1.0'}
category = Categorias._get_by_name('Productos')
next_key = Productos.next_key()['value']
unit = SATUnidades.get_by_name(args['unit'])
fields = dict(
categoria = category,
clave = next_key,
clave_sat = args['key_sat'],
descripcion = args['description'],
unidad = unit,
valor_unitario = Decimal(args['unit_value']),
inventario = True,
es_activo = True,
)
obj = Productos.create(**fields)
where = (SATImpuestos.activo==True & SATImpuestos.default==True)
taxes = SATImpuestos.select().where(where)
obj.impuestos = taxes
obj.existencia += cant
obj.save()
return obj
def _add_entries(self, partner, products):
def _add_entries(self, products):
msg = ''
for p in products:
print(type(p), p)
product = self._get_or_create_product(self, p)
cant = float(p['cant'])
product = self._get_or_create_product(self, p, cant)
values = dict(
product = product,
cant = cant,
)
InventoryEntries.create(**values)
msg = 'Validando'
return msg
def _create(self, args):
@ -9516,7 +9529,7 @@ class InventoryEntries(BaseModel):
# ~ ToDo - Save invoice
error = self._add_entries(self, partner, args['products'])
error = self._add_entries(self, args['products'])
if error:
result = {'ok': False, 'msg': error}
return result

View File

@ -465,6 +465,7 @@ function _add_entries_inventory(data){
success:function(text, data, XmlHttpRequest){
var values = data.json();
if (values.ok) {
get_products()
$$('multi_invoices').setValue('products_home')
$$('lbl_partner').setValue('')
grid.clearAll()
@ -492,6 +493,7 @@ function cmd_save_products_add_click(id, e, node){
p.id_product = r.id_product
p.key_sat = r.key_sat1
p.description = r.description1
p.unit = r.unit
p.unit_value = r.unit_value1
p.cant = r.cant1
products.push(p)