Skip to content

Commit

Permalink
Merge pull request #70 from dev-lymar/tony_makedoc
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-lymar committed Jun 18, 2024
2 parents 95e89b0 + 7384ad8 commit c24fe87
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions makedoc/services.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import zipfile
from datetime import date, datetime
from tempfile import NamedTemporaryFile
from io import BytesIO

import openpyxl
from openpyxl.styles import Alignment, Border, Font, Side
Expand Down Expand Up @@ -33,7 +33,11 @@ def update_documents(self):
if self.validated_data["delivery_cost"] > 0:
self.transport_sheet = 1

if max(self.validated_data.get("items"), key=lambda x: x["discount"])["discount"] > 0:
have_discount = max(self.validated_data.get("items"), key=lambda x: x["discount"])[
"discount"
]

if have_discount > 0:
self.service_note = 1

def form_auto_document(self, request):
Expand Down Expand Up @@ -62,7 +66,7 @@ def form_auto_document(self, request):

self.apply_styles(ws)

self.add_workbook_to_archive(wb, user, client)
self.add_workbook_to_archive_xlsx(wb, user, client)

def fill_contract_info(self, ws, client):
formatted_contract_date = client.contract_date.strftime("%d.%m.%Y")
Expand Down Expand Up @@ -186,24 +190,24 @@ def fill_manager_contact(self, ws, user):
)
phone.alignment = Alignment(horizontal="center", vertical="center")

def add_workbook_to_archive(self, wb, user, client):
directory = os.path.join("makedoc", "tempdoc", user.full_name)
os.makedirs(directory, exist_ok=True)
def add_workbook_to_archive_xlsx(self, wb, user, client):
tempdir = os.path.join("makedoc", "tempdoc", str(user.id))
os.makedirs(tempdir, exist_ok=True)

self.archive_name = f"{directory}/\
{client.client_name} {datetime.today().strftime('%d.%m.%Y %H:%M:%S')}.zip"
self.archive_name = (
f"{client.client_name} {datetime.today().strftime('%d.%m.%Y %H:%M:%S')}.zip"
)
archive_path = f"{tempdir}/{self.archive_name}"

filename = f"{client.last_application_number} {self.docname} \
{client.client_name} {date.today().strftime('%d.%m.%Y')}.xlsx"
xlsx_filename = f"{client.last_application_number} {self.docname} {client.client_name} \
{date.today().strftime('%d.%m.%Y')}.xlsx"

with NamedTemporaryFile(delete=True) as tmp:
wb.save(tmp.name)
tmp.seek(0)
stream = tmp.read()
tmp.close()
xlsx_io = BytesIO()
wb.save(xlsx_io)
xlsx_io.seek(0)

with zipfile.ZipFile(self.archive_name, "a") as archive:
archive.writestr(filename, stream)
with zipfile.ZipFile(archive_path, "a") as archive:
archive.writestr(xlsx_filename, xlsx_io.getvalue())

def apply_styles(self, ws):
for row in ws.iter_rows():
Expand Down Expand Up @@ -243,7 +247,7 @@ def form_rw_document(self, request):

self.apply_styles(ws)

self.add_workbook_to_archive(wb, user, client)
self.add_workbook_to_archive_xlsx(wb, user, client)

def fill_rw_services(self, ws, factory, client, logistics):
caret = self.caret_factory
Expand Down Expand Up @@ -328,11 +332,15 @@ def form_service_note(self, request):
self.fill_product_info(ws, product, logistics, 22)
self.apply_styles(ws)

self.add_workbook_to_archive(wb, user, client)
self.add_workbook_to_archive_xlsx(wb, user, client)

def fill_text_note(self, ws, client, discount, destination):
region = destination.split(",")[1].strip()
ws.cell(row=15, column=1, value=f"{get_formatted_date_agreement()} № 12/2.2/23/3-")
blank = "________"
if destination != "0":
region = destination.split(",")[1].strip()
else:
destination, region = blank, blank
ws.cell(row=15, column=1, value=f"{get_formatted_date_agreement()} № 12/2.2/23/3-___")
# Нужно правильлно разобрать в json регион чтобы склонять только название
text = f" В целях увеличения объема продаж на территории {region} прошу Вашего \
согласования применить скидку для контрагента {client.client_name} ({destination}) до {discount}%\
Expand Down Expand Up @@ -367,7 +375,7 @@ def form_transport_sheet(self, request):

self.apply_styles(ws)

self.add_workbook_to_archive(wb, user, client)
self.add_workbook_to_archive_xlsx(wb, user, client)

def fill_contract_info_transport_sheet(self, ws, client):
formatted_contract_date = client.contract_date.strftime("%d.%m.%Y")
Expand Down

0 comments on commit c24fe87

Please sign in to comment.