Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import from path #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions setari/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def post(self, request):
dim2.insert_incasari()
dim2.insert_cheltuieli()
dim2.insert_documente()
dim2.insert_facturi()

messages.add_message(
request,
Expand All @@ -55,7 +56,6 @@ def post(self, request):
return redirect("/setari/")



class SetariView(View):

def get(self, request):
Expand All @@ -71,7 +71,6 @@ def post(self, request, *args, **kwargs):
form = SetariForm(request.POST)

if not form.is_valid():
print("------", form.errors)
return render(
request,
"setari.html",
Expand Down Expand Up @@ -99,17 +98,32 @@ def post(self, request):
storage_path = os.path.join(extracts_path, "stocare")
os.makedirs(storage_path, exist_ok=True)

def save_model_to_csv(model, filename):
copyfile = lambda fp: shutil.copy2(fp, os.path.join(storage_path, os.path.basename(fp)))

def save_model_to_csv(model, filename: str):
data = model.objects.all()

if len(data) == 0:
return

for item in data:
shutil.copy2(item.fisier.path, os.path.join(storage_path, os.path.basename(item.fisier.path)))

if hasattr(item, "fisier"):
if item.fisier:
copyfile(item.fisier.path)
if hasattr(item, "fisier_efactura_xml"):
if item.fisier_efactura_xml:
copyfile(item.fisier_efactura_xml.path)
if hasattr(item, "fisier_factura_pdf"):
if item.fisier_factura_pdf:
copyfile(item.fisier_factura_pdf.path)

df = pd.DataFrame(data.values())
df.to_csv(os.path.join(storage_path, filename), index=False)

save_model_to_csv(CheltuialaModel, "Cheltuiala.csv")
save_model_to_csv(IncasariModel, "Incasari.csv")
save_model_to_csv(DocumenteModel, "Documente.csv")
save_model_to_csv(FacturaModel, "Factura.csv")

df = pd.DataFrame(SetariModel.objects.all().values())
df.to_csv(os.path.join(storage_path, "Setari.csv"), index=False)
Expand Down
2 changes: 1 addition & 1 deletion static/versiune.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.2
2.0.3
6 changes: 2 additions & 4 deletions templates/setari.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ <h1>⚙️ Setari</h1>
source: { local: localitati }
});">
<label>
Alege localitatea:
<input name="localitate" {% if setari_form.localitate.value %}
value="{{setari_form.localitate.value}}" {% endif %} x-ref="searchInputEl" autocomplete="off"
type="text" placeholder="Iasi...">
<span>Alege localitatea:</span>
<input name="localitate" {% if setari_form.localitate.value %} value="{{ setari_form.localitate.value }}" {% endif %} x-ref="searchInputEl" autocomplete="off" type="text" placeholder="Iasi...">
{{ setari_form.localitate.errors }}
</label>
</div>
Expand Down
66 changes: 66 additions & 0 deletions utils/data_import_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from incasari.models import IncasariModel
from cheltuieli.models import CheltuialaModel
from setari.models import SetariModel
from facturi.models import FacturaModel
from django.utils import timezone
from datetime import datetime
from core.settings import get_media_path
Expand Down Expand Up @@ -143,3 +144,68 @@ def insert_documente(self):
parse_tip_document=False,
)
instance.save()

def insert_facturi(self):

df = pd.read_csv(os.path.join("stocare", "Factura.csv"))
df = df.astype(object).where(df.notna(), None)
records = df.to_dict('records')
media_path = get_media_path()

for record in records:
filepath_fisier_factura_pdf = os.path.join("stocare", record["filepath_fisier_factura_pdf"])
# TODO
if filepath_fisier_efactura_xml:
filepath_fisier_efactura_xml = os.path.join("stocare", record["filepath_fisier_efactura_xml"])
shutil.copy2(filepath_fisier_efactura_xml, os.path.join(media_path, record["filepath_fisier_efactura_xml"]))

if filepath_fisier_efactura_xml:
shutil.copy2(filepath_fisier_efactura_xml, os.path.join(media_path, record["filepath_fisier_efactura_xml"]))


instance = FacturaModel(
# serie = models.CharField(max_length=20)
# numar = models.IntegerField()
# data_emitere = models.DateTimeField(default=timezone.now)
# data_scadenta = models.DateTimeField(default=one_month_from_now)
# tip_factura = models.CharField(
# max_length=300,
# choices=TipFactura,
# default=TipFactura.E_FACTURA,
# )
# # Client
# nume = models.CharField(max_length=200)
# cif = models.CharField(max_length=50)
# nr_reg_com = models.CharField(max_length=50, null=True, blank=True)
# localitate = models.CharField(
# max_length=250,
# choices=Localitati,
# null=True,
# blank=True,
# default=Localitati.LOCALITATE_GENERIC,
# )
# adresa = models.CharField(max_length=1000)
# email = models.EmailField(max_length=250, null=True, blank=True)
# telefon = models.CharField(max_length=250, null=True, blank=True)
# # Plata
# tip_tranzactie = models.CharField(
# max_length=250, choices=ModalitatePlata, default=ModalitatePlata.BANCAR
# )
# total_de_plata = models.FloatField()
# valuta = models.CharField(max_length=3, choices=Valuta, default=Valuta.RON)
# # Produse sau servicii
# # id, numar_unitati, total_de_plata, nume_produs_sau_serviciu, cod_unitate, pret_pe_unitate, subtotal
# produse_sau_servicii = models.JSONField(default=list)
# nota = models.TextField(max_length=5000, null=True, blank=True)
# data_inserarii = models.DateTimeField(default=timezone.now, null=True, blank=True)

# # Fisiere
# fisier_efactura_xml = models.FileField(
# max_length=100_000, upload_to=get_save_path, null=True, blank=True
# )
# fisier_factura_pdf = models.FileField(
# max_length=100_000, upload_to=get_save_path, null=True, blank=True
# )
)
instance.save()