Ejemplo n.º 1
0
def new_delivery_receipt_view(request):
        
    data = get_abstract_data()
    
    data['current_delivery_id'] = DeliveryReceipt.objects.last().id + 1 if DeliveryReceipt.objects.exists() else 1
    
    receipt_getter = ReceiptGetter()
    
    data['new_delivery_receipt_serials'] = get_receipts_data('delivery', 'serial')
    data['new_delivery_receipt_companies'] = receipt_getter.get_companies('delivery')
    data['new_delivery_receipt_types'] = receipt_getter.get_types('delivery')
    data['inner_reprs'] = receipt_getter.get_representatives('inner')
    data['outer_reprs'] = receipt_getter.get_representatives('outer')
    data['data'] = dict(request.GET)
    data['company'] = request.GET.get('company', '')
    
    return render(request, 'receipts/new-delivery-receipt.html', context=data)
def create_delivery_receipt(request):

    if request.is_ajax():

        data = request.POST

        receipt_data = json.loads(data['data'])
        company = data['company']

        date = datetime.datetime.strptime(
            data['date'],
            '%d/%m/%Y').replace(tzinfo=pendulum.timezone(settings.TIME_ZONE))

        inner_representative = data['innerRepresentative']
        outer_representative = data['outerRepresentative']

        not_existing_serials = []

        for item in receipt_data:

            qty = item['qty']
            device_type = item['type']
            serial_numbers = item['serialNumbers']

            for serial in serial_numbers:

                inventory_device = InventoryDevice.objects.filter(
                    reception_receipt__company_name=company,
                    serial_number=serial,
                    device_type=device_type,
                    deleted=False)

                if not inventory_device.exists():
                    not_existing_serials.append(serial)

        if not_existing_serials:

            message = 'الأرقام التالية غير متطابقة البيانات أو غير موجودة:<br>'

            for index, serial in enumerate(not_existing_serials):
                message += '- {}<br>'.format(serial)

            return JsonResponse({'not_existing_serials': message},
                                status=status.HTTP_400_BAD_REQUEST)

        delivery_receipt = DeliveryReceipt.objects.create(
            company_name=company,
            date=date,
            inner_representative=inner_representative,
            outer_representative=outer_representative)

        devices = []

        for item in receipt_data:

            qty = item['qty']
            type_ = item['type']
            serial_numbers = item['serialNumbers']

            for serial in serial_numbers:

                maintenance_device = get_object_or_None(
                    MaintenanceDevice, inventory_device__serial_number=serial)
                inventory_device = InventoryDevice.objects.filter(
                    serial_number=serial).last()

                inventory_device.delivered = True
                inventory_device.save()

                if maintenance_device:
                    maintenance_device.deleted = True
                    maintenance_device.save()

                archive_device = delivery_receipt.devices.create(
                    inventory_device=inventory_device, leave_date=date)

                devices.append(archive_device.as_dict())

        receipt_getter = ReceiptGetter()

        return JsonResponse({
            'receipt':
            delivery_receipt.as_dict(),
            'devices':
            devices,
            'reception_companies':
            receipt_getter.get_companies('delivery'),
            'delivery_companies':
            receipt_getter.get_companies('reception'),
            'reception_types':
            receipt_getter.get_types('reception'),
            'delivery_types':
            receipt_getter.get_types('delivery'),
            'inner_representatives':
            receipt_getter.get_representatives('inner'),
            'outer_representatives':
            receipt_getter.get_representatives('outer')
        })
def create_reception_receipt(request):

    if request.is_ajax():

        data = request.POST

        receipt_data = json.loads(data['data'])
        company = data['company']
        date = datetime.datetime.strptime(
            data['date'],
            '%d/%m/%Y').replace(tzinfo=pendulum.timezone(settings.TIME_ZONE))

        inner_representative = data['innerRepresentative']
        outer_representative = data['outerRepresentative']

        existing_serials = []
        non_matching_serials = []
        all_serials = []
        duplicate_serials = []
        errors = {}

        for item in receipt_data:

            device_type = item['type']
            serial_numbers = item['serialNumbers']

            for serial in serial_numbers:
                if serial not in all_serials:
                    all_serials.append(serial)

                elif serial not in duplicate_serials:
                    duplicate_serials.append(serial)

                inventory_devices = InventoryDevice.objects.filter(
                    serial_number=serial, deleted=False, delivered=False)
                archive_devices = ArchiveDevice.objects.filter(
                    inventory_device__serial_number=serial)

                if inventory_devices.exists():
                    existing_serials.append(serial)

                if archive_devices.exists():
                    first_device = archive_devices.first()

                    if company != first_device.delivery_receipt.company_name or device_type != first_device.inventory_device.device_type:
                        non_matching_serials.append(serial)

        if existing_serials:

            existing_serials_message = 'الأرقام التالية موجودة بالفعل:<br>'

            for index, serial in enumerate(existing_serials):
                existing_serials_message += '- {}<br>'.format(serial)

            errors['existing_serials_message'] = existing_serials_message

        if non_matching_serials:

            non_matching_serials_message = 'الأرقام التالية موجودة فى الأرشيف ببيانات مختلفة:<br>'

            for serial in non_matching_serials:
                non_matching_serials_message += '- {}<br>'.format(serial)

            errors[
                'non_matching_serials_message'] = non_matching_serials_message

        if duplicate_serials:
            duplicate_serials_message = 'الارقام التالية مكررة: <br>'

            for serial in duplicate_serials:
                duplicate_serials_message += '- {}<br>'.format(serial)

            errors['duplicate_serials_message'] = duplicate_serials_message

        if errors:
            return JsonResponse(errors, status=status.HTTP_400_BAD_REQUEST)

        reception_receipt = ReceptionReceipt.objects.create(
            company_name=company,
            date=date,
            inner_representative=inner_representative,
            outer_representative=outer_representative)

        devices = []

        for item in receipt_data:

            qty = item['qty']
            device_type = item['type']
            serial_numbers = item['serialNumbers']

            for serial in serial_numbers:

                inventory_device = reception_receipt.devices.create(
                    serial_number=serial,
                    device_type=device_type,
                    entrance_date=date)

                devices.append(inventory_device.as_dict())

        receipt_getter = ReceiptGetter()

        return JsonResponse({
            'receipt':
            reception_receipt.as_dict(),
            'devices':
            devices,
            'reception_companies':
            receipt_getter.get_companies('reception'),
            'delivery_companies':
            receipt_getter.get_companies('delivery'),
            'reception_types':
            receipt_getter.get_types('reception'),
            'delivery_types':
            receipt_getter.get_types('delivery'),
            'inner_representatives':
            receipt_getter.get_representatives('inner'),
            'outer_representatives':
            receipt_getter.get_representatives('outer'),
        })