diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index e907e72b..33247728 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -28,7 +28,7 @@ from wtforms import ( from wtforms.fields import FormField from ereuse_devicehub.db import db -from ereuse_devicehub.inventory.models import Transfer +from ereuse_devicehub.inventory.models import DeliveryNote, ReceiverNote, Transfer from ereuse_devicehub.parser.models import SnapshotsLog from ereuse_devicehub.parser.parser import ParseSnapshotLsHw from ereuse_devicehub.parser.schemas import Snapshot_lite @@ -1183,3 +1183,73 @@ class EditTransferForm(TransferForm): def set_obj(self, commit=True): self.populate_obj(self._obj) + + +class NotesForm(FlaskForm): + number = StringField( + 'Number', + [validators.Optional()], + render_kw={'class': "form-control"}, + description="You can put a number for tracer of receiver or delivery", + ) + date = DateField( + 'Date', + [validators.Optional()], + render_kw={'class': "form-control"}, + description="""Date when the transfer was do it""", + ) + units = IntegerField( + 'Units', + [validators.Optional()], + render_kw={'class': "form-control"}, + description="Number of units", + ) + weight = IntegerField( + 'Weight', + [validators.Optional()], + render_kw={'class': "form-control"}, + description="Weight expressed in Kg", + ) + + def __init__(self, *args, **kwargs): + self.type = kwargs.pop('type', None) + lot_id = kwargs.pop('lot_id', None) + self._tmp_lot = Lot.query.filter(Lot.id == lot_id).one() + self._obj = None + super().__init__(*args, **kwargs) + + if self._tmp_lot.transfer: + if self.type == 'Delivery': + self._obj = self._tmp_lot.transfer.delivery_note + if not self._obj: + self._obj = DeliveryNote(transfer_id=self._tmp_lot.transfer.id) + + self.date.description = """Date when the delivery was do it.""" + self.number.description = ( + """You can put a number for tracer of delivery note.""" + ) + + if self.type == 'Receiver': + self._obj = self._tmp_lot.transfer.receiver_note + if not self._obj: + self._obj = ReceiverNote(transfer_id=self._tmp_lot.transfer.id) + + self.date.description = """Date when the receipt was do it.""" + self.number.description = ( + """You can put a number for tracer of receiber note.""" + ) + + if self._obj and not self.data['csrf_token']: + self.number.data = self._obj.number + self.date.data = self._obj.date + self.units.data = self._obj.units + self.weight.data = self._obj.weight + + def save(self, commit=True): + self.populate_obj(self._obj) + db.session.add(self._obj) + + if commit: + db.session.commit() + + return self._obj diff --git a/ereuse_devicehub/inventory/models.py b/ereuse_devicehub/inventory/models.py index 9c18fc0e..69414c10 100644 --- a/ereuse_devicehub/inventory/models.py +++ b/ereuse_devicehub/inventory/models.py @@ -44,7 +44,7 @@ class Transfer(Thing): return False -class Notes(Thing): +class DeliveryNote(Thing): id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) number = Column(CIText(), default='', nullable=False) date = Column(db.TIMESTAMP(timezone=True)) @@ -56,19 +56,27 @@ class Notes(Thing): db.ForeignKey('transfer.id'), nullable=False, ) - - -class DeliveryNote(Notes): transfer = relationship( 'Transfer', backref=backref('delivery_note', lazy=True, uselist=False, cascade=CASCADE_OWN), - primaryjoin='Notes.transfer_id == Transfer.id', + primaryjoin='DeliveryNote.transfer_id == Transfer.id', ) -class ReceiverNote(Notes): +class ReceiverNote(Thing): + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) + number = Column(CIText(), default='', nullable=False) + date = Column(db.TIMESTAMP(timezone=True)) + units = Column(Integer, default=0) + weight = Column(Integer, default=0) + + transfer_id = db.Column( + UUID(as_uuid=True), + db.ForeignKey('transfer.id'), + nullable=False, + ) transfer = relationship( 'Transfer', backref=backref('receiver_note', lazy=True, uselist=False, cascade=CASCADE_OWN), - primaryjoin='Notes.transfer_id == Transfer.id', + primaryjoin='ReceiverNote.transfer_id == Transfer.id', ) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 51cf36b7..4569a13a 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -21,6 +21,7 @@ from ereuse_devicehub.inventory.forms import ( LotForm, NewActionForm, NewDeviceForm, + NotesForm, TagDeviceForm, TradeDocumentForm, TradeForm, @@ -57,6 +58,8 @@ class DeviceListMixin(GenericMixin): lot = lots.filter(Lot.id == lot_id).one() if not lot.is_temporary and lot.transfer: form_transfer = EditTransferForm(lot_id=lot.id) + form_delivery = NotesForm(lot_id=lot.id, type='Delivery') + form_receiver = NotesForm(lot_id=lot.id, type='Receiver') form_new_action = NewActionForm(lot=lot_id) self.context.update( @@ -67,6 +70,8 @@ class DeviceListMixin(GenericMixin): 'form_new_allocate': AllocateForm(lot=lot_id), 'form_new_datawipe': DataWipeForm(lot=lot_id), 'form_transfer': form_transfer, + 'form_delivery': form_delivery, + 'form_receiver': form_receiver, 'form_filter': form_filter, 'form_print_labels': PrintLabelsForm(), 'lot': lot, @@ -631,6 +636,42 @@ class SnapshotDetailView(GenericMixin): ) +class DeliveryNoteView(GenericMixin): + methods = ['POST'] + form_class = NotesForm + + def dispatch_request(self, lot_id): + self.get_context() + form = self.form_class(request.form, lot_id=lot_id, type='Delivery') + next_url = url_for('inventory.lotdevicelist', lot_id=lot_id) + + if form.validate_on_submit(): + form.save() + messages.success('Transfer updated successfully!') + return flask.redirect(next_url) + + messages.error('Transfer updated error!') + return flask.redirect(next_url) + + +class ReceiverNoteView(GenericMixin): + methods = ['POST'] + form_class = NotesForm + + def dispatch_request(self, lot_id): + self.get_context() + form = self.form_class(request.form, lot_id=lot_id, type='Receiver') + next_url = url_for('inventory.lotdevicelist', lot_id=lot_id) + + if form.validate_on_submit(): + form.save() + messages.success('Transfer updated successfully!') + return flask.redirect(next_url) + + messages.error('Transfer updated error!') + return flask.redirect(next_url) + + devices.add_url_rule('/action/add/', view_func=NewActionView.as_view('action_add')) devices.add_url_rule('/action/trade/add/', view_func=NewTradeView.as_view('trade_add')) devices.add_url_rule( @@ -693,3 +734,11 @@ devices.add_url_rule( '/lot//transfer/', view_func=EditTransferView.as_view('edit_transfer'), ) +devices.add_url_rule( + '/lot//deliverynote/', + view_func=DeliveryNoteView.as_view('deliverynote'), +) +devices.add_url_rule( + '/lot//receivernote/', + view_func=ReceiverNoteView.as_view('receivernote'), +) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index d08bc413..d3f71ffe 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -90,6 +90,16 @@ Transfer ({% if lot.transfer.closed %}Closed{% else %}Open{% endif %}) + + {% endif %} @@ -498,6 +508,64 @@ +
+
Delivery Note
+
+ {{ form_delivery.csrf_token }} + + {% for field in form_delivery %} + {% if field != form_delivery.csrf_token %} +
+ {% if field != form_delivery.type %} + {{ field.label(class_="form-label") }} + {{ field }} + {% if field.errors %} +

+ {% for error in field.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %} + {% endif %} +
+ {% endif %} + {% endfor %} + +
+ Cancel + +
+
+
+
+
Receiver Note
+
+ {{ form_receiver.csrf_token }} + + {% for field in form_receiver %} + {% if field != form_receiver.csrf_token %} +
+ {% if field != form_receiver.type %} + {{ field.label(class_="form-label") }} + {{ field }} + {% if field.errors %} +

+ {% for error in field.errors %} + {{ error }}
+ {% endfor %} +

+ {% endif %} + {% endif %} +
+ {% endif %} + {% endfor %} + +
+ Cancel + +
+
+
{% endif %}