xlsx_stock.py 23.9 KB
Newer Older
Patrick's avatar
Patrick committed
1 2 3 4 5 6 7 8 9 10 11 12
# -*- coding: utf-8
from __future__ import unicode_literals

from django.db import transaction
from django.db.models import Q
from django.utils import translation
from django.utils.translation import ugettext_lazy as _
from openpyxl import load_workbook
from openpyxl.style import Fill
from openpyxl.styles import Color

import repanier.apps
chris's avatar
chris committed
13
from repanier.xlsx.export_tools import *
Patrick's avatar
Patrick committed
14
from repanier.const import *
Patrick's avatar
Patrick committed
15 16
from repanier.models.offeritem import OfferItem
from repanier.models.product import Product
Patrick's avatar
Patrick committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
from repanier.tools import update_offer_item, next_row
from repanier.xlsx.import_tools import get_row, get_header


def export_permanence_stock(permanence, deliveries_id=None, customer_price=False, wb=None, ws_customer_title=None):
    if wb is not None:
        yellowFill = Fill()
        yellowFill.start_color.index = 'FFEEEE11'
        yellowFill.end_color.index = 'FFEEEE11'
        yellowFill.fill_type = Fill.FILL_SOLID

        header = [
            (_("Id"), 5),
            (_("OfferItem"), 5),
            (_("Reference"), 20),
            (_("Product"), 60),
            (_("customer unit price") if customer_price else _("producer unit price"), 10),
            (_("Deposit"), 10),
            (_("Asked"), 10),
            (_("quantity ordered"), 10),
            (_("Initial stock"), 10),
            (repanier.apps.REPANIER_SETTINGS_CURRENCY_DISPLAY, 15),
            (_("Stock used"), 10),
            (_("Add 2 stock"), 10),
            (_("Final stock"), 10),
            (repanier.apps.REPANIER_SETTINGS_CURRENCY_DISPLAY, 15),
        ]
        offer_items = OfferItem.objects.filter(
            Q(
                permanence_id=permanence.id,
                manage_replenishment=True,
                translations__language_code=translation.get_language()
            ) |
            Q(
                permanence_id=permanence.id,
                manage_production=True,
                translations__language_code=translation.get_language()
            )
        ).order_by(
            "producer",
            "translations__long_name",
            "order_average_weight",
        ).select_related(
            'producer', 'department_for_customer'
        ).iterator()
        offer_item = next_row(offer_items)
        if offer_item is not None:
            # Check if there are deliveries_ws
            deliveries_ws = []
            if deliveries_id is not None:
                for delivery_cpt, delivery_id in enumerate(deliveries_id):
                    ws_sc_name = cap('%d-%s' % (delivery_cpt, ws_customer_title), 31)
                    for sheet in wb.worksheets:
                        if ws_sc_name == sheet.title:
                            deliveries_ws.append(ws_sc_name)
                            break
            else:
Patrick's avatar
Patrick committed
74
                ws_sc_name = cap(slugify(ws_customer_title), 31)
Patrick's avatar
Patrick committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
                for sheet in wb.worksheets:
                    if ws_sc_name == sheet.title:
                        deliveries_ws.append(ws_sc_name)
                        break
            wb, ws = new_landscape_a4_sheet(
                wb,
                _('Stock check'),
                permanence,
                header
            )
            formula_main_total_a = []
            formula_main_total_b = []
            show_column_reference = False
            show_column_qty_ordered = False
            show_column_add2stock = False
            row_num = 1
            while offer_item is not None:
                producer_save = offer_item.producer
                row_start_producer = row_num + 1
                c = ws.cell(row=row_num, column=2)
                c.value = producer_save.short_profile_name
                c.style.font.bold = True
                c.style.font.italic = True
                while offer_item is not None and producer_save.id == offer_item.producer_id:
                    department_for_customer_save__id = offer_item.department_for_customer_id
                    department_for_customer_save__short_name = offer_item.department_for_customer.short_name \
                        if offer_item.department_for_customer is not None else None
                    while offer_item is not None and producer_save.id == offer_item.producer_id \
                            and department_for_customer_save__id == offer_item.department_for_customer_id:
                        if len(offer_item.reference) < 36:
Patrick's avatar
Patrick committed
105 106 107 108 109
                            if offer_item.reference.isdigit():
                                # Avoid display of exponent by Excel
                                offer_item_reference = '[%s]' % offer_item.reference
                            else:
                                offer_item_reference = offer_item.reference
Patrick's avatar
Patrick committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
                            show_column_reference = True
                        else:
                            offer_item_reference = EMPTY_STRING
                        if offer_item.order_unit < PRODUCT_ORDER_UNIT_DEPOSIT:

                            asked = offer_item.quantity_invoiced - offer_item.add_2_stock
                            stock = offer_item.stock
                            add_2_stock = offer_item.add_2_stock
                            c = ws.cell(row=row_num, column=0)
                            c.value = offer_item.producer_id
                            c = ws.cell(row=row_num, column=1)
                            c.value = offer_item.id
                            c = ws.cell(row=row_num, column=2)
                            c.value = offer_item_reference
                            c.style.number_format.format_code = NumberFormat.FORMAT_TEXT
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c = ws.cell(row=row_num, column=3)
                            if department_for_customer_save__short_name is not None:
                                c.value = "%s - %s" % (
                                    offer_item.get_long_name(is_quantity_invoiced=True),
                                    department_for_customer_save__short_name
                                )
                            else:
                                c.value = "%s" % offer_item.get_long_name(is_quantity_invoiced=True)
                            c.style.number_format.format_code = NumberFormat.FORMAT_TEXT
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c = ws.cell(row=row_num, column=4)
                            unit_price = offer_item.customer_unit_price if customer_price else offer_item.producer_unit_price
                            c.value = unit_price.amount
                            c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c = ws.cell(row=row_num, column=5)
                            c.value = offer_item.unit_deposit.amount
                            c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c = ws.cell(row=row_num, column=6)
                            if ws_customer_title is None:
                                c.value = asked
                            else:
                                if len(deliveries_ws) > 0:
                                    #     # Without any deleveriy point
                                    #     sum_value = "SUMIF('%s'!B2:B5000,B%s,'%s'!F2:F5000)" % \
                                    #         (ws_customer_title, row_num + 1, ws_customer_title)
                                    # else:
                                    sum_value = "+".join("SUMIF('%s'!B2:B5000,B%s,'%s'!F2:F5000)" % \
                                                         (delivery_ws, row_num + 1, delivery_ws)
                                                         for delivery_ws in deliveries_ws
                                                         )
                                    c.value = "=%s" % sum_value
                                else:
                                    c.value = DECIMAL_ZERO
                            c.style.number_format.format_code = '#,##0.???'
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c = ws.cell(row=row_num, column=7)
                            c.value = '=G%s-K%s+L%s' % (row_num + 1, row_num + 1, row_num + 1)
                            if not show_column_qty_ordered:
                                show_column_qty_ordered = (asked - min(asked, stock) + add_2_stock) > 0
                            c.style.number_format.format_code = '#,##0.???'
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c = ws.cell(row=row_num, column=8)
                            c.value = stock
                            c.style.number_format.format_code = '#,##0.???'
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c.style.font.color = Color(Color.BLUE)
                            ws.conditional_formatting.addCellIs(
                                get_column_letter(9) + str(row_num + 1), 'notEqual',
                                [str(stock)], True, wb,
                                None, None, yellowFill
                            )
                            c = ws.cell(row=row_num, column=9)
                            c.value = '=ROUND(I%s*(E%s+F%s),2)' % (row_num + 1, row_num + 1, row_num + 1)
                            c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c = ws.cell(row=row_num, column=10)
                            c.value = '=MIN(G%s,I%s)' % (row_num + 1, row_num + 1)
                            c.style.number_format.format_code = '#,##0.???'
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c = ws.cell(row=row_num, column=11)
                            c.value = add_2_stock
                            c.style.number_format.format_code = '#,##0.???'
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c.style.font.color = Color(Color.BLUE)
                            ws.conditional_formatting.addCellIs(
                                get_column_letter(12) + str(row_num + 1), 'notEqual',
                                [str(add_2_stock)], True, wb,
                                None, None, yellowFill
                            )
                            if not show_column_add2stock:
                                show_column_add2stock = add_2_stock > 0
                            c = ws.cell(row=row_num, column=12)
                            c.value = '=I%s-K%s+L%s' % (row_num + 1, row_num + 1, row_num + 1)
                            c.style.number_format.format_code = '#,##0.???'
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            c.style.font.bold = True
                            c = ws.cell(row=row_num, column=13)
                            c.value = '=ROUND(M%s*(E%s+F%s),2)' % (row_num + 1, row_num + 1, row_num + 1)
                            c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
                            c.style.borders.bottom.border_style = Border.BORDER_THIN
                            row_num += 1
                        offer_item = next_row(offer_items)
                row_num += 1
                c = ws.cell(row=row_num, column=3)
                c.value = "%s %s" % (_("Total price"), producer_save.short_profile_name)
                c.style.number_format.format_code = NumberFormat.FORMAT_TEXT
                c.style.font.bold = True
                c.style.alignment.horizontal = c.style.alignment.HORIZONTAL_RIGHT
                c = ws.cell(row=row_num, column=9)
                formula = 'SUM(J%s:J%s)' % (row_start_producer, row_num)
                c.value = '=' + formula
                c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
                c.style.font.bold = True
                formula_main_total_a.append(formula)
                c = ws.cell(row=row_num, column=13)
                formula = 'SUM(N%s:N%s)' % (row_start_producer, row_num)
                c.value = '=' + formula
                c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
                c.style.font.bold = True
                formula_main_total_b.append(formula)

                if offer_items is not None:
                    # Display a separator line between producers
                    row_num += 1
                    for col_num in range(16):
                        c = ws.cell(row=row_num, column=col_num)
                        c.style.borders.bottom.border_style = Border.BORDER_MEDIUMDASHED
                    row_num += 2

            c = ws.cell(row=row_num, column=3)
            c.value = "%s" % _("Total price")
            c.style.number_format.format_code = NumberFormat.FORMAT_TEXT
            c.style.font.bold = True
            c.style.alignment.horizontal = c.style.alignment.HORIZONTAL_RIGHT
            c = ws.cell(row=row_num, column=9)
            c.value = "=" + "+".join(formula_main_total_a)
            c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
            c.style.font.bold = True
            c = ws.cell(row=row_num, column=13)
            c.value = "=" + "+".join(formula_main_total_b)
            c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
            c.style.font.bold = True

            row_num += 1
            for col_num in range(16):
                c = ws.cell(row=row_num, column=col_num)
                c.style.borders.bottom.border_style = Border.BORDER_MEDIUMDASHED

            ws.column_dimensions[get_column_letter(1)].visible = False
            ws.column_dimensions[get_column_letter(2)].visible = False
            ws.column_dimensions[get_column_letter(11)].visible = False
            if not show_column_reference:
                ws.column_dimensions[get_column_letter(3)].visible = False
            if not show_column_qty_ordered:
                ws.column_dimensions[get_column_letter(8)].visible = False
            if not show_column_add2stock:
                ws.column_dimensions[get_column_letter(12)].visible = False
    return wb


# @transaction.atomic
# def import_stock_sheet(worksheet, permanence=None):
#     error = False
#     error_msg = None
#     if permanence.status < PERMANENCE_DONE:
#         header = get_header(worksheet)
#         if header:
#             row_num = 1
#             row = get_row(worksheet, header, row_num)
#             while row and not error:
#                 try:
#                     # with transaction.atomic():
#                     stock = None if row[_('Initial stock')] is None else Decimal(row[_('Initial stock')]).quantize(THREE_DECIMALS)
#                     add_2_stock = None if row[_('Add 2 stock')] is None else Decimal(row[_('Add 2 stock')]).quantize(THREE_DECIMALS)
#                     if stock is not None:
#                         producer_id = None if row[_('Id')] is None else Decimal(row[_('Id')])
#                         offer_item_id = None if row[_('OfferItem')] is None else Decimal(row[_('OfferItem')])
#                         offer_item = OfferItem.objects.filter(
#                             id=offer_item_id,
#                             permanence_id=permanence.id,
#                             producer_id=producer_id
#                         ).order_by('?').first()
#                         if offer_item is not None \
#                                 and (offer_item.stock != stock or offer_item.add_2_stock != add_2_stock):
#                             offer_item.stock = stock
#                             offer_item.add_2_stock = add_2_stock
#                             offer_item.save()
#                             Product.objects.filter(
#                                 id=offer_item.product_id,
#                                 producer_id=producer_id
#                             ).update(stock=stock)
#                     row_num += 1
#                     row = get_row(worksheet, header, row_num)
#                 except KeyError, e:
#                     # Missing field
#                     error = True
#                     error_msg = _("Row %(row_num)d : A required column is missing.") % {'row_num': row_num + 1}
#                 except Exception, e:
#                     error = True
#                     error_msg = _("Row %(row_num)d : %(error_msg)s.") % {'row_num': row_num + 1, 'error_msg': str(e)}
#     else:
#         error = True
#         error_msg = _("The status of this permanence prohibit you to update the stock.")
#     return error, error_msg


def export_producer_stock(producers, customer_price=False, wb=None):
    yellowFill = Fill()
    yellowFill.start_color.index = 'FFEEEE11'
    yellowFill.end_color.index = 'FFEEEE11'
    yellowFill.fill_type = Fill.FILL_SOLID

    header = [
        (_("Id"), 5),
        (_("Producer"), 60),
        (_("Reference"), 20),
        (_("Product"), 60),
        (_("customer unit price") if customer_price else _("producer unit price"), 10),
        (_("Deposit"), 10),
        (_("Current stock"), 10),
        (repanier.apps.REPANIER_SETTINGS_CURRENCY_DISPLAY, 15),
    ]
    producers = producers.iterator()
    producer = next_row(producers)
    wb, ws = new_landscape_a4_sheet(
        wb,
        _('Current stock'),
        _('Current stock'),
        header
    )
    show_column_reference = False
    row_num = 1
    while producer is not None:
        products = Product.objects.filter(
            producer_id=producer.id,
            is_active=True,
            translations__language_code=translation.get_language()
        ).order_by(
            "translations__long_name",
            "order_average_weight",
        ).select_related(
            'producer', 'department_for_customer'
        ).iterator()
        product = next_row(products)
        while product is not None:
            if product.order_unit < PRODUCT_ORDER_UNIT_DEPOSIT:
                c = ws.cell(row=row_num, column=0)
                c.value = product.id
                c = ws.cell(row=row_num, column=1)
                c.value = "%s" % product.producer
                if len(product.reference) < 36:
Patrick's avatar
Patrick committed
359 360 361 362 363
                    if product.reference.isdigit():
                        # Avoid display of exponent by Excel
                        product_reference = '[%s]' % product.reference
                    else:
                        product_reference = product.reference
Patrick's avatar
Patrick committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
                    show_column_reference = True
                else:
                    product_reference = EMPTY_STRING
                c = ws.cell(row=row_num, column=2)
                c.value = product_reference
                c.style.number_format.format_code = NumberFormat.FORMAT_TEXT
                c.style.borders.bottom.border_style = Border.BORDER_THIN
                c = ws.cell(row=row_num, column=3)
                if product.department_for_customer is not None:
                    c.value = "%s - %s" % (
                        product.get_long_name(),
                        product.department_for_customer.short_name
                    )
                else:
                    c.value = product.get_long_name()
                c.style.number_format.format_code = NumberFormat.FORMAT_TEXT
                c.style.borders.bottom.border_style = Border.BORDER_THIN
                c = ws.cell(row=row_num, column=4)
                unit_price = product.customer_unit_price if customer_price else product.producer_unit_price
                c.value = unit_price.amount
                c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
                c.style.borders.bottom.border_style = Border.BORDER_THIN
                c = ws.cell(row=row_num, column=5)
                c.value = product.unit_deposit.amount
                c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
                c.style.borders.bottom.border_style = Border.BORDER_THIN
                c = ws.cell(row=row_num, column=6)
                c.value = product.stock
                c.style.number_format.format_code = '_ * #,##0.00_ ;_ * -#,##0.00_ ;_ * "-"??_ ;_ @_ '
                c.style.font.color = Color(Color.BLUE)
                c.style.borders.bottom.border_style = Border.BORDER_THIN
                c = ws.cell(row=row_num, column=7)
                c.value = '=ROUND((E%s+F%s)*G%s,2)' % (row_num + 1, row_num + 1, row_num + 1)
                c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
                ws.conditional_formatting.addCellIs(
                    get_column_letter(8) + str(row_num + 1), 'notEqual',
                    [str(((unit_price.amount + product.unit_deposit.amount) * product.stock).quantize(TWO_DECIMALS))],
                    True, wb,
                    None, None, yellowFill
                )
                c.style.borders.bottom.border_style = Border.BORDER_THIN
                row_num += 1
            product = next_row(products)
        row_num += 1
        c = ws.cell(row=row_num, column=4)
        c.value = "%s" % (_("Total"),)
        c.style.number_format.format_code = NumberFormat.FORMAT_TEXT
        c.style.font.bold = True
        c.style.alignment.horizontal = c.style.alignment.HORIZONTAL_RIGHT
        c = ws.cell(row=row_num, column=7)
        formula = 'SUM(H%s:H%s)' % (2, row_num)
        c.value = '=' + formula
        c.style.number_format.format_code = repanier.apps.REPANIER_SETTINGS_CURRENCY_XLSX
        c.style.font.bold = True

        ws.column_dimensions[get_column_letter(1)].visible = False
        if not show_column_reference:
            ws.column_dimensions[get_column_letter(3)].visible = False
        producer = next_row(producers)
    return wb


@transaction.atomic
def import_producer_stock(worksheet, producers=None):
    error = False
    error_msg = None
    header = get_header(worksheet)
    if header:
        row_num = 1
        row = get_row(worksheet, header, row_num)
        while row and not error:
            try:
                # with transaction.atomic():
                product_id = None if row[_('Id')] is None else Decimal(row[_('Id')])
                if product_id is not None:
                    stock = DECIMAL_ZERO if row[_('Current stock')] is None else Decimal(
                        row[_('Current stock')]).quantize(
                        THREE_DECIMALS)
                    stock = stock if stock >= DECIMAL_ZERO else DECIMAL_ZERO
                    Product.objects.filter(
                        id=product_id, producer__in=producers
                    ).update(stock=stock)
                update_offer_item(product_id=product_id)
                row_num += 1
                row = get_row(worksheet, header, row_num)
Patrick's avatar
Patrick committed
449
            except KeyError as e:
Patrick's avatar
Patrick committed
450 451 452
                # Missing field
                error = True
                error_msg = _("Row %(row_num)d : A required column is missing.") % {'row_num': row_num + 1}
Patrick's avatar
Patrick committed
453
            except Exception as e:
Patrick's avatar
Patrick committed
454 455 456 457 458
                error = True
                error_msg = _("Row %(row_num)d : %(error_msg)s.") % {'row_num': row_num + 1, 'error_msg': str(e)}
    return error, error_msg


Patrick's avatar
Patrick committed
459
def handle_uploaded_stock(request, producers, file_to_import, *args):
Patrick's avatar
Patrick committed
460 461 462 463
    error = False
    error_msg = None
    wb = load_workbook(file_to_import)
    if wb is not None:
Patrick's avatar
Patrick committed
464
        ws = wb.get_sheet_by_name(cap(slugify("%s" % _('Current stock')), 31))
Patrick's avatar
Patrick committed
465 466 467 468 469 470 471 472
        if ws is not None:
            error, error_msg = import_producer_stock(
                ws,
                producers=producers
            )
            if error:
                error_msg = cap("%s" % _('Current stock'), 31) + " > " + error_msg
    return error, error_msg