Пример #1
0
    def request_submit(cls, delivery: Delivery, config):

        if config['City'] not in delivery.to.settlement and config['City'] not in delivery.to.region:
            return False

        data = {
            'matter': config['Matter'],
            'total_weight_kg': 1,
            'insurance_amount': str(delivery.order.items_cost),
            'payment_method': 'non_cash',
            'points': [
                {
                    'address': config['WarehouseAddress'],
                    'contact_person': {
                        'phone': config['WarehousePhone']
                    }
                },
                {
                    'address': delivery.to.value,
                    'contact_person': {
                        'phone': filter_phone(delivery.recipient.phone)
                    }
                }
            ]
        }

        response = requests.post(
            cls.endpoint + '/create-order',
            headers={
                'Content-Type': 'application/json',
                'X-DV-Auth-Token': config['AuthToken']
            },
            data=json.dumps(data).encode('utf-8')
        )
        result = response.json()

        if response.status_code == 200:
            delivery.external_id = result['order']['order_id']
            delivery.status = Delivery.SUBMITTED
            delivery.save(update_fields=['external_id', 'status'])
        else:
            return None
Пример #2
0
    def request_delivery_draft(cls, delivery: Delivery, config):
        optimal_option = cls.request_optimal(delivery.type, delivery.to,
                                             delivery.order.items_cost, config)
        assert optimal_option is not None

        complete_address = cls.get_complete_address(delivery.to, config)
        if not complete_address:
            log.error('Unknown address for delivery')
            delivery.status = Delivery.ERROR
            delivery.save(update_fields=['status'])
            return False

        pickup_point_id = None
        if delivery.type.type in ['POST', 'COURIER']:
            if delivery.type.extra['useClosestPickup']:
                pickup_point_id = cls.get_closest_pickup(
                    delivery, optimal_option['pickupPointIds'],
                    delivery.to.latitude, delivery.to.longitude, config)
            else:
                pickup_point_id = delivery.extra['pickupPointId']

        package = Package.objects.for_delivery(delivery)
        if not package:
            log.error("No matching package")
            delivery.status = Delivery.ERROR
            delivery.save(update_fields=['status'])
            return False

        shipment_date = cls.get_shipment_date().isoformat()

        data = {
            'senderId':
            config['ShopId'],
            'externalId':
            delivery.order.id
            if not settings.DEBUG else f'DEBUG_{delivery.order.id}',
            'comment':
            f'Доставка по заказу {delivery.order.id}',
            'deliveryType':
            delivery.type.type,
            'recipient':
            convert_recipient(delivery.recipient, delivery.to,
                              complete_address['geoId'], pickup_point_id),
            'cost': {
                'manualDeliveryForCustomer': 0,
                'paymentMethod': 'PREPAID',
                'assessedValue': delivery.order.items_cost,
                'fullyPrepaid': True
            },
            'contacts': [{
                'type': 'RECIPIENT',
                'phone': delivery.recipient.phone,
                'firstName': delivery.recipient.first_name,
                'lastName': delivery.recipient.last_name,
            }],
            'deliveryOption':
            convert_option(optimal_option),
            'shipment': {
                'type': 'WITHDRAW',
                'date': shipment_date,
                'warehouseFrom': config['WarehouseId'],
                'partnerTo': optimal_option['shipments'][0]['partner']['id'],
            },
            'places': [{
                'externalId':
                delivery.order.id,
                'dimensions':
                convert_package(package),
                'items': [
                    convert_order_item(order_item) for order_item in
                    delivery.order.items.prefetch_related('item').all()
                ]
            }]
        }

        response = requests.post(cls.endpoint + '/orders',
                                 headers={
                                     'Content-Type': 'application/json',
                                     'Authorization': config['OAuth']
                                 },
                                 data=json.dumps(data).encode('utf-8'))

        if response.status_code == 200:
            delivery.external_id = response.json()
            delivery.extra['shipment_id'] = int(
                optimal_option['shipments'][0]['partner']['id'])
            delivery.extra['shipment_date'] = shipment_date
            delivery.save(update_fields=['status', 'external_id', 'extra'])
            return True
        else:
            log.error(
                f'Ошибка интеграции Яндекс.Доставки по заказу {delivery.order.id}: {response.json()}'
            )
            delivery.status = Delivery.ERROR
            delivery.save(update_fields=['status'])
            return False