Calculate Commissions

0

I need to calculate the commission a doctor must have for the laboratory tests he sends his patients. In other words, the laboratory pays you for each order sent.

Here I have the commission parameterization table:

class Comisiones(models.Model):
  precio_inicial = models.IntegerField()
  precio_final = models.IntegerField()
  porcentaje_comision = models.IntegerField()
  doctor = models.ForeignKey(Doctores, on_delete=models.CASCADE)

  def full_name(self):
        return u'{} []'.format(self.precio_inicial, self.precio_final, self.porcentaje_comision)

Here I have the table where the order is registered:

class OrdenPaciente(models.Model):
   paciente =  models.ForeignKey(Pacientes, on_delete=models.CASCADE)
   doctor = models.ForeignKey(Doctores, on_delete=models.CASCADE)
   estado_paciente = models.CharField(max_length=50)
   status = models.BooleanField(default=True)
   fecha_solicitud = models.DateField(blank=True, null=True)
   dias = models.CharField(max_length=3)
   precio = models.IntegerField()

Here the table where the Commissions are registered

class ComisionesCalculo(models.Model):
   orden = models.ForeignKey(OrdenPaciente, on_delete=models.CASCADE)
   comision = models.ForeignKey(Comisiones, on_delete=models.CASCADE)
   calculo = models.DecimalField(default=0, max_digits=5, decimal_places=2)

   def full_name(self):
        return u'{} []'.format(self.orden, self.comision, self.calculo)

Someone helps me how I could calculate the commission based on the price and commission percentage parameterized in the Commissions model

    
asked by Roberto Feijoo 08.08.2018 в 18:48
source

1 answer

0

That's how I solved it

 def calcular_comisiones(id):
   orden = OrdenPaciente.objects.get(id=id)
   try:
       comision = Comisiones.objects.get(doctor_id=orden.doctor_id,precio_final__gte=orden.precio,precio_inicial__lte=orden.precio)
       calculo = ((orden.precio)*(comision.porcentaje_comision))/100
       comcal = ComisionesCalculo()
       comcal.ordenid_id = id
       comcal.comision_id = comision.id
       comcal.calculo = calculo
       comcal.paciente_id = orden.paciente_id
       comcal.doctor_id = orden.doctor_id
       comcal.save()

  except Comisiones.DoesNotExist:
       pass
  return HttpResponse(True)
    
answered by 09.08.2018 / 00:41
source