Beispiel #1
0
 def create(self, validated_data):
     lineas = [linea.save() for linea in validated_data['lineas']]
     neto = sum(linea.importe_neto for linea in lineas)
     gravado = Gravado.objects.get(pk=validated_data['gravado_id'])
     responsable = validated_data['responsable']
     tipo_comprobante = TipoComprobante.objects.get(
         pk=validated_data['tipo_comprobante_id'])
     iva = neto * gravado.porcentaje / Decimal("100.00")
     total = neto + iva
     del validated_data["lineas"]
     comprobante = Comprobante(
         estado=Comprobante.NO_COBRADO,
         numero=0,  # el numero nos lo va a dar la afip cuando emitamos
         fecha_emision=date.today(),
         fecha_recepcion=date.today(),
         total_facturado=total,
         total_cobrado=0,  # Para que no explote el azul
         nro_terminal=CEDIR_PTO_VENTA
         if responsable == "Cedir" else BRUNETTI_PTO_VENTA,
         **validated_data)
     for linea in lineas:
         linea.comprobante = comprobante
     Afip().emitir_comprobante(comprobante, lineas)
     comprobante.save()
     for linea in lineas:
         linea.comprobante = comprobante
         linea.save()
     return comprobante
Beispiel #2
0
def import_cbte_csv(csvfile):
    errors = []
    lineerrs = []
    linen = 0
    empresa = Empresa.objects.first()
    prefetched = _prefetch(empresa)
    try:
        with transaction.atomic():
            records = csv.DictReader(csvfile,
                                     fieldnames=cbte_import_fields,
                                     restval=None)
            last_item_number = None
            last_cbte = None

            for record in records:
                linen += 1
                lineerrs = []
                if not record["cbte_item_number"]:
                    lineerrs.append(
                        "la linea {} del archivo no tiene un numero de item".
                        format(linen))
                    continue
                if record["cbte_item_number"] != last_item_number:
                    last_cbte = None
                    last_item_number = record["cbte_item_number"]
                    # creo cbte
                    tipo_cbte = get_or_error(lineerrs, linen, record,
                                             "cbte_tipo_cbte",
                                             prefetched[TipoComprobante],
                                             "tipo de comprobante")
                    punto_vta = get_or_error(lineerrs, linen, record,
                                             "cbte_punto_vta",
                                             prefetched[PuntoDeVenta],
                                             "punto de venta")
                    concepto = get_or_error(lineerrs, linen, record,
                                            "cbte_concepto",
                                            prefetched[Concepto], "concepto")
                    nro_remito = check_str(lineerrs, linen, record,
                                           "cbte_nro_remito", 13,
                                           "numero de remito")
                    fecha_emision = check_date(lineerrs, linen, record,
                                               "cbte_fecha_emision",
                                               "fecha de emision")
                    fecha_vto = check_date(lineerrs, linen, record,
                                           "cbte_fecha_vencimiento",
                                           "fecha de vencimiento de pago")
                    tipo_doc = get_or_error(lineerrs, linen, record,
                                            "cbte_id_tipo_doc_cliente",
                                            prefetched[TipoDoc],
                                            "tipo de documento del cliente")
                    nro_doc = check_str(lineerrs, linen, record,
                                        "cbte_nro_doc_cliente", 128,
                                        "numero de documento del cliente")

                    tipo_expo = get_or_error(lineerrs,
                                             linen,
                                             record,
                                             "cbte_tipo_exportacion",
                                             prefetched[TipoExportacion],
                                             "tipo de exportacion",
                                             opcional=True)

                    moneda = get_or_error(lineerrs,
                                          linen,
                                          record,
                                          "cbte_id_moneda",
                                          prefetched[Moneda],
                                          "moneda",
                                          default="PES")

                    cotizaccion = check_float(lineerrs,
                                              linen,
                                              record,
                                              "cbte_moneda_ctz",
                                              "cotizacion",
                                              default=1)
                    idioma = get_or_error(lineerrs,
                                          linen,
                                          record,
                                          "cbte_id_idioma",
                                          prefetched[Idioma],
                                          "idioma",
                                          default=1)
                    incoterms = get_or_error(lineerrs,
                                             linen,
                                             record,
                                             "cbte_id_incoterms",
                                             prefetched[Incoterms],
                                             "incoterms",
                                             opcional=True)
                    incoterms_ds = check_str(lineerrs, linen, record,
                                             "cbte_incoterms_ds", 256,
                                             "incoterms - descripcion")
                    pais_destino = get_or_error(lineerrs,
                                                linen,
                                                record,
                                                "cbte_id_pais_destino",
                                                prefetched[PaisDestino],
                                                "pais de destino",
                                                opcional=True)

                    id_impositivo = check_str(lineerrs, linen, record,
                                              "cbte_id_impositivo", 256,
                                              "id impositivo")

                    obs_comerciales = check_str(
                        lineerrs, linen, record,
                        "cbte_observaciones_comerciales", 9999,
                        "observaciones comerciales")
                    obs = check_str(lineerrs, linen, record,
                                    "cbte_observaciones", 9999,
                                    "observaciones")
                    forma_pago = check_str(lineerrs, linen, record,
                                           "cbte_forma_pago", 256,
                                           "forma de pago")
                    condicion_vta = get_or_error(lineerrs, linen, record,
                                                 "cbte_id_condicion_vta",
                                                 prefetched[CondicionVenta],
                                                 "condicion de venta")
                    condicion_vta_texto = check_str(
                        lineerrs, linen, record, "cbte_condicion_vta_otra",
                        256, "condicion de venta - otra")

                    # Info cliente
                    nombre = check_str(lineerrs, linen, record,
                                       "cbte_cliente_nombre", 256,
                                       "razon social del cliente")
                    condicion_iva = get_or_error(
                        lineerrs, linen, record, "cbte_cliente_condicion_iva",
                        prefetched[CondicionIva],
                        "condicion de iva del cliente")
                    domicilio = check_str(lineerrs, linen, record,
                                          "cbte_cliente_domicilio", 256,
                                          "domicilio del cliente")
                    localidad = check_str(lineerrs, linen, record,
                                          "cbte_cliente_localidad", 256,
                                          "localidad del cliente")
                    telefono = check_str(lineerrs, linen, record,
                                         "cbte_cliente_telefono", 256,
                                         "telefono del cliente")
                    cod_postal = check_str(lineerrs, linen, record,
                                           "cbte_cliente_cp", 256,
                                           "codigo postal del cliente")
                    email = check_str(lineerrs, linen, record,
                                      "cbte_cliente_email", 75,
                                      "email del cliente")

                    if (tipo_doc.id_afip, nro_doc) in prefetched[Cliente]:
                        cliente = prefetched[Cliente][(tipo_doc.id_afip,
                                                       nro_doc)]
                        if cliente_changed(cliente, nombre, condicion_iva,
                                           domicilio, localidad, telefono,
                                           cod_postal, email):
                            cliente.nombre = nombre
                            cliente.condicion_iva = condicion_iva
                            cliente.domicilio = domicilio
                            cliente.localidad = localidad
                            cliente.telefono = telefono
                            cliente.cod_postal = cod_postal
                            cliente.email = email
                            cliente.save()
                    else:
                        cliente = Cliente(tipo_doc=tipo_doc,
                                          nro_doc=nro_doc,
                                          nombre=nombre,
                                          condicion_iva=condicion_iva,
                                          domicilio=domicilio,
                                          localidad=localidad,
                                          telefono=telefono,
                                          cod_postal=cod_postal,
                                          email=email)
                        cliente.save()
                        prefetched[Cliente][(tipo_doc.id_afip,
                                             nro_doc)] = cliente

                    last_cbte = Comprobante(
                        empresa=empresa,
                        tipo_cbte=tipo_cbte,
                        punto_vta=punto_vta,
                        concepto=concepto,
                        remito_nro=nro_remito,
                        fecha_emision=fecha_emision,
                        fecha_venc_pago=fecha_vto,
                        cliente_id=cliente.id,
                        tipo_expo=tipo_expo,
                        moneda=moneda,
                        idioma=idioma,
                        incoterms=incoterms,
                        pais_destino=pais_destino,
                        id_impositivo=id_impositivo,
                        moneda_ctz=Decimal(cotizaccion),
                        observaciones_comerciales=obs_comerciales,
                        observaciones=obs,
                        forma_pago=forma_pago,
                        incoterms_ds=incoterms_ds,
                        condicion_venta=condicion_vta,
                        condicion_venta_texto=condicion_vta_texto,
                    )
                    last_cbte.save()

                if record["det_nombre"] or record["det_codigo"]:
                    codigo = check_str(lineerrs, linen, record, "det_codigo",
                                       128, "codigo de producto")
                    cantidad = check_float(lineerrs, linen, record, "det_cant",
                                           "cantidad")

                    if codigo:
                        if codigo in prefetched[Producto]:
                            prod = prefetched[Producto][codigo]
                        else:
                            nombre = check_str(lineerrs, linen, record,
                                               "det_nombre", 256,
                                               "texto del detalle")
                            precio_unit = check_float(lineerrs, linen, record,
                                                      "det_precio_unit",
                                                      "precio unitario")
                            unidad = get_or_error(lineerrs, linen, record,
                                                  "det_unidad",
                                                  prefetched[Unidad],
                                                  "unidad de medida")
                            alicuota = get_or_error(lineerrs, linen, record,
                                                    "det_id_alicuota",
                                                    prefetched[AlicuotaIva],
                                                    "alicuota IVA")
                            prod = Producto(codigo=codigo,
                                            nombre=nombre,
                                            precio_unit=Decimal(precio_unit),
                                            alicuota_iva=alicuota,
                                            unidad=unidad)
                            prefetched[Producto][codigo] = prod
                            prod.save()
                        det = DetalleComprobante(
                            comprobante_id=last_cbte.id,
                            producto_id=prod.id,
                            cant=Decimal(cantidad),
                            detalle=prod.nombre_completo,
                            precio_unit=prod.precio_unit,
                            alicuota_iva=prod.alicuota_iva,
                            unidad=prod.unidad)
                    else:
                        nombre = check_str(lineerrs, linen, record,
                                           "det_nombre", 256,
                                           "texto del detalle")
                        precio_unit = check_float(lineerrs, linen, record,
                                                  "det_precio_unit",
                                                  "precio unitario")
                        unidad = get_or_error(lineerrs, linen, record,
                                              "det_unidad", prefetched[Unidad],
                                              "unidad de medida")
                        alicuota = get_or_error(lineerrs, linen, record,
                                                "det_id_alicuota",
                                                prefetched[AlicuotaIva],
                                                "alicuota IVA")
                        det = DetalleComprobante(
                            comprobante_id=last_cbte.id,
                            cant=Decimal(cantidad),
                            detalle=nombre,
                            precio_unit=Decimal(precio_unit),
                            alicuota_iva=alicuota,
                            unidad=unidad)

                    det.save()

                if record["trib_id"]:
                    tributo = get_or_error(lineerrs, linen, record, "trib_id",
                                           prefetched[Tributo], "tributo")
                    detalle = check_str(lineerrs, linen, record,
                                        "trib_detalle", 256,
                                        "detalle del tributo")
                    base_imponible = check_float(lineerrs, linen, record,
                                                 "trib_base_imponible",
                                                 "base imponible")
                    alicuota = check_float(lineerrs, linen, record,
                                           "trib_alicuota", "alicuota")

                    trib = TributoComprobante(
                        comprobante_id=last_cbte.id,
                        tributo_id=tributo.id,
                        detalle=detalle,
                        base_imponible=Decimal(base_imponible),
                        alicuota=Decimal(alicuota))
                    trib.save()
                if record["op_id"]:
                    opcional = get_or_error(lineerrs, linen, record, "op_id",
                                            prefetched[Opcional], "opcional")
                    valor = check_str(lineerrs, linen, record, "op_valor", 256,
                                      "valor del opcional")

                    op = OpcionalComprobante(comprobante_id=last_cbte.id,
                                             opcional_id=opcional.id,
                                             valor=valor)
                    op.save()

                errors.extend(lineerrs)
                lineerrs = []
        if errors:
            raise Exception()

    except Exception as e:
        errors.extend(lineerrs)
        errors.append(
            "Hubo un error al procesar el archivo. Ultima linea procesada: {}".
            format(linen or 0))
        logger.exception("Error al importar")

    return errors