def shipping_of_packet( request): shipper = 'MRW' json_data=json.loads(request.body) orders_draw = json_data.get('orders') amount=json_data.get('amount') guide_shipping = json_data.get('guide_shipping') orders = Order.objects.filter(store_order_id__in=orders_draw) if len(orders) != len(orders_draw): bad_orders = set(orders_draw) for order in orders: bad_orders.discard(order.store_order_id) return JsonResponse({ 'ok':False, 'msg': f'El envio {guide_shipping}. No se pudo registrar porque la(s) siguiente(s) \ ordene(s) no corresponden con nuestra base de datos. \n{bad_orders}' }) destination = orders[0].destination_place for order in orders: if destination != order.destination_place: destination = '' break if not destination: return JsonResponse({ 'ok':False, 'msg': f'{request.user.first_name}. La guia {guide_shipping} contiene ordenes que debian\ ser enviadas a direcciones distintas. Contacta urgentemente a un supervisor.' }) request_shipping = new_shipping(guide_shipping,amount,shipper,destination) if not request_shipping.get('ok'): return JsonResponse(request_shipping) shipping=request_shipping.get('data') bulk_mgr = BulkCreateManager() msg = f'Paquete enviado bajo en numero de guia: {guide_shipping}' for orden in orders: order.state=Order.INTERNATIONAL_DEPARTURE order.shipping = shipping bulk_mgr.update(order,{'state','shipping'}) New.objects.create( user=request.user, message=msg, order=order ) bulk_mgr.done() return JsonResponse({ 'ok':True, 'msg':f'Envio {guide_shipping}, Registrado con exito.' })
def update_products(self, list_ids): logging.getLogger('log_three').info( f'Actualizando {len(list_ids)} productos. \n') products = Product.objects.filter(provider_sku__in=list_ids) path = '/items' params = [{ 'ids': ids, 'attributes': 'id,price,initial_quantity,status,currency_id', } for ids in self.split_ids(list_ids)] results = self.map_pool_get([path] * len(params), params) products_draw = { product['body']['id']: product['body'] for product in results if product.get('body') } BM = BusinessModel.objects.get(pk=self.SELLER_ID) bulk_mgr = BulkCreateManager() for product in products: id = product.provider_sku if id in products_draw: if not products_draw[id].get('initial_quantity'): logging.getLogger('log_three').warning( f'Producto {id} NO ACTUALIZADO') continue if not products_draw[id]['status'] == 'active': products_draw[id]['initial_quantity'] = 0 logging.getLogger('log_three').info( f"{product}: quantity: {product.quantity} \ -> {products_draw[id]['initial_quantity']}") product.quantity = products_draw[id]['initial_quantity'] if products_draw[id]['currency_id'] == 'USD': cost_price = ceil(products_draw[id]['price']) else: cost_price = ceil(products_draw[id]['price'] / BM.trm) sale_cost = ceil( (cost_price + BM.shipping_vzla) * (1 + BM.meli_tax / 100) * (1 + BM.utility / 100)) product.cost_price = cost_price product.sale_price = sale_cost product.last_update = timezone.localtime() bulk_mgr.update( product, {'quantity', 'cost_price', 'sale_price', 'last_update'}) bulk_mgr.done()
def received_packet( request, order_id): _order = Order.objects.filter(store_order_id=order_id).select_related('shipping').first() if not _order: return JsonResponse({ 'ok':False, 'msg': 'Error pedido no encontrado.' }) shipping = _order.shipping result = shipment_completed(shipping) if not result.get('ok'): return JsonResponse(result) orders = Order.objects.filter(shipping=shipping).select_related('product').select_related('product__product') bulk_mgr = BulkCreateManager() number_products = 0 msg = 'El paquete donde se envio el producto fue recibido.' for order in orders: New.objects.create( user=request.user, message=msg, order=order ) order.state=Order.RECEIVED_STORE number_products += order.quantity bulk_mgr.update(order, {'state'}) bulk_mgr.done() message = f'Solicitud Exitosa. El paquete contenia {number_products} producto(s).\ A continuacion se listan los numeros de pedidos con sus respectivos paquetes productos:' for order in orders: message += f'\nPedido: {order.store_order_id} -> {order.quantity} {order.product.product.title}.' return JsonResponse({ 'ok':True, 'msg': message })