from django.db import models
from datetime import datetime


class TypeLaunch(models.Model):
    type_name = models.CharField(max_length=100, unique=True)


class Provider(models.Model):
    description = models.CharField(max_length=100, unique=True)
    type_launch = models.ForeignKey(TypeLaunch, blank=True, null=True)
    date_last_purchase = models.DateTimeField('date last purchase')
    value_total = models.DecimalField(max_digits=5, decimal_places=2)


class Extract(models.Model):
    date_launch = models.DateTimeField('date launch')
    launch = models.CharField(max_length=100)
    date_purchase = models.DateTimeField('date purchase')
    value_debit = models.DecimalField(max_digits=5, decimal_places=2)
    value_credit = models.DecimalField(max_digits=5, decimal_places=2)
    value_balance = models.DecimalField(max_digits=5, decimal_places=2)
    cancelled = models.BooleanField(default=True, db_index=True)
    provider = models.ForeignKey(Provider, blank=True, null=True)

    def str_to_date(self, date, year='2014'):
        #import pdb; pdb.set_trace()
        if launch.strip()[-3] == '/':
            
        date = date.split('-')[-1].strip()
        date = date.replace('/','-')
        if len(date) == 5:
            date = date + '-' + str(year)

        return datetime.strptime(date, '%d-%m-%Y').date()

    def str_to_float(self, value):
        return float(value.replace(',','.'))

    def importer(self, path):
        with open(path, 'r') as ff:
            contents = ff.readlines()
            line = 0
            extract = Extract()
            while line <= len(contents):
                date_launch, launch, value = contents[line].split(';')
                extract.date_launch = extract.str_to_date(date_launch)
                extract.launch = launch.strip()   #.split('-')[:-1]
                
                    year = extract.str_to_date(date_launch).year
                    extract.date_purchase = extract.str_to_date(launch, year)
                else:
                    extract.date_purchase = extract.str_to_date(date_launch)

                if extract.str_to_float(value) < 0:
                    extract.value_debit = extract.str_to_float(value)
                    extract.value_credit = 0
                else:
                    extract.value_debit = 0
                    extract.value_credit = extract.str_to_float(value)

                extract.value_balance = 0
                extract.save()
                line += 1

            ff.close()














@@ -1021,21 +1021,29 @@
 date
-, year='2014'
+_launch, launch, year
 ):%0A 
@@ -1128,36 +1128,29 @@
         
-%0A        
 date = 
-date
+launch
 .split('
@@ -1165,32 +1165,36 @@
 strip()%0A        
+ 
+   
 date = date.repl
@@ -1209,59 +1209,8 @@
 '-')
-%0A        if len(date) == 5:%0A            date = date
  + '
@@ -1676,39 +1676,56 @@
 h = 
-extract.str_to_date(date_launch
+datetime.strptime(date_launch, '%25d-%25m-%25Y').date(
 )%0A  
@@ -1806,37 +1806,16 @@
         
-%0A                    
 year = e
@@ -1859,36 +1859,32 @@
                 
-   
- 
 extract.date_pur
@@ -1911,24 +1911,29 @@
 to_date(
+date_
 launch, 
 year)%0A  
@@ -1928,111 +1928,20 @@
 ch, 
-year)%0A                else:%0A                    extract.date_purchase = extract.str_to_date(date_
 launch
+, year
 )%0A%0A 
@@ -2370,18 +2370,4 @@
 se()
-%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A

fix models finance with date lauch