File size: 3,133 Bytes
cf8de8a
 
24bdb4d
 
 
 
 
 
b0a47bb
24bdb4d
 
 
b0a47bb
24bdb4d
 
 
 
b0a47bb
 
24bdb4d
b0a47bb
 
 
24bdb4d
 
 
 
e102c92
24bdb4d
b0a47bb
 
 
24bdb4d
e102c92
24bdb4d
 
b0a47bb
 
 
 
24bdb4d
b0a47bb
e102c92
24bdb4d
 
b0a47bb
 
24bdb4d
b0a47bb
e102c92
24bdb4d
b0a47bb
 
 
24bdb4d
b0a47bb
e102c92
24bdb4d
b0a47bb
24bdb4d
b0a47bb
 
 
24bdb4d
 
 
 
b0a47bb
24bdb4d
e102c92
24bdb4d
b0a47bb
 
 
24bdb4d
e102c92
b417c45
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from ...models import Orden, Posicion, Producto, Tecnica
from ...utils import controller_error
from django.db import DatabaseError


class OrdenesController():
    products: list[Producto]
    technique: Tecnica
    raw_list_orders: list[dict]
    orders: list[Orden]
    positions: list[Posicion]

    def __init__(self, raw_orders: list[dict], list_products: list[Producto], technique: Tecnica):
        self.products = list_products
        self.technique = technique
        self.raw_list_orders = raw_orders

    def serRawOrders(self, new_raw_orders: list[dict]):
        self.raw_list_orders = new_raw_orders

    def setOrdersToSave(self):
        self.orders = []
        for raw in self.raw_list_orders:
            self.orders.append(Orden(id_tecnica=self.technique))

    def saveOrders(self):
        if not self.orders:
            return controller_error("No se han establecido las órdenes para guardar")
        try:
            for order in self.orders:
                order.save()
            return self.orders
        except DatabaseError as error:
            return controller_error("Error al guardar las ordenes")

    def setPositions(self):
        codes_ids_products = {}
        for product in self.products:
            codes_ids_products[product.codigoProducto] = product.id
        codes_expect = list(codes_ids_products.keys())

        if len(self.orders) != len(self.raw_list_orders):
            return controller_error("El número de ordenes guardados no coinciden con los recibidos")

        self.positions = []
        for index, order in enumerate(self.raw_list_orders):
            received_codes_order = list(order.keys())

            if set(received_codes_order) != set(codes_expect):
                return controller_error("Las ordenes mandadas no contienen los productos esperados")

            for name, position_index in order.items():
                list_product_use = [product for product in self.products
                                    if product.codigoProducto == name]

                if len(list_product_use) != 1:
                    return controller_error("No pueden existir dos productos que ocupen la misma posición de un orden")

                product_use = list_product_use[0]
                new_position = Posicion(
                    id_producto=product_use,
                    id_orden=self.orders[index],
                    posicion=position_index
                )
                self.positions.append(new_position)
        return self.positions

    def savePositions(self):
        if not self.positions:
            return controller_error("No se han establecido posiciones para guarguar")
        try:
            for position in self.positions:
                position.save()
            return self.positions
        except DatabaseError as error:
            return controller_error("Error al guardar las posiciones")

    @staticmethod
    def getOrderById(id: int):
        try:
            order = Orden.objects.get(id=id)
            return order
        except Orden.DoesNotExist:
            return controller_error("No existe orden")