Esempio n. 1
0
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)
            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)}
Esempio n. 2
0
def import_producer_stock(worksheet):
    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[_("Inventory")] is None else Decimal(
                                 row[_("Inventory")]).quantize(THREE_DECIMALS))
                    stock = stock if stock >= DECIMAL_ZERO else DECIMAL_ZERO
                    Product.objects.filter(id=product_id).update(stock=stock)
                update_offer_item(product_id=product_id)
                row_num += 1
                row = get_row(worksheet, header, row_num)
            except KeyError as e:
                # Missing field
                error = True
                error_msg = _(
                    "Row %(row_num)d : A required column is missing.") % {
                        "row_num": row_num + 1
                    }
            except Exception as e:
                error = True
                error_msg = _("Row %(row_num)d : %(error_msg)s.") % {
                    "row_num": row_num + 1,
                    "error_msg": str(e),
                }
    return error, error_msg
Esempio n. 3
0
def import_invoice_sheet(worksheet, invoice_reference=EMPTY_STRING,
                         customer_2_id_dict=None,
                         producer=None
                         ):
    error = False
    error_msg = None
    header = get_header(worksheet)
    if header:
        now = timezone.now().date()
        lut_reverse_vat = dict(LUT_ALL_VAT_REVERSE)
        import_counter = 0
        row_num = 1
        sid = transaction.savepoint()
        try:

            permanence = Permanence.objects.create(
                permanence_date=now,
                short_name=invoice_reference,
                status=PERMANENCE_SEND,
                highest_status=PERMANENCE_SEND
            )
            permanence.producers.add(producer)
            row = get_row(worksheet, header, row_num)
            while row and not error:
                customer_name = row[_("Customer")]
                if customer_name:
                    if customer_name in customer_2_id_dict:
                        customer_id = customer_2_id_dict[customer_name]
                    else:
                        error = True
                        error_msg = _("Row %(row_num)d : No valid customer") % {'row_num': row_num + 1}
                        break
                    product_reference = row[_("Reference")] or EMPTY_STRING
                    unit = row[_("Unit")]
                    order_unit = get_reverse_invoice_unit(unit)
                    vat = row[_("VAT level")]
                    vat_level = lut_reverse_vat[vat]
                    product = Product.objects.filter(producer_id=producer.id, reference=product_reference).order_by(
                        '?').first()
                    if product is None:
                        product = Product.objects.create(
                            producer=producer,
                            reference=product_reference,
                        )
                    long_name = row[_("Product")]
                    # The producer unit price is the imported customer unit price
                    # If the group get a reduction, this one must be mentioned into the producer admin screen
                    # into the "price_list_multiplier" field
                    product.producer_unit_price = row[_("Customer unit price")]
                    product.unit_deposit = row[_("Deposit")]
                    product.order_unit = order_unit
                    product.vat_level = vat_level
                    product.wrapped = row[_("Wrapped")]
                    qty_and_price_display = product.get_qty_and_price_display(customer_price=False)
                    if long_name.endswith(qty_and_price_display):
                        long_name = long_name[:-len(qty_and_price_display)]
                    product.long_name = long_name[:100]
                    product.save()
                    offer_item = product.get_or_create_offer_item(permanence)
                    create_or_update_one_purchase(
                        customer_id=customer_id,
                        offer_item=offer_item,
                        status=PERMANENCE_SEND,
                        q_order=Decimal(row[_("Quantity")]),
                        batch_job=True, is_box_content=False,
                        comment=row[_("Comment")] or EMPTY_STRING
                    )
                    import_counter += 1

                row_num += 1
                row = get_row(worksheet, header, row_num)
            reorder_offer_items(permanence.id)
            reorder_purchases(permanence.id)

        except KeyError as e:
            # Missing field
            error = True
            error_msg = _("Row %(row_num)d : A required column is missing %(error_msg)s.") % {
                'row_num': row_num + 1, 'error_msg': str(e)}
        except Exception as e:
            error = True
            error_msg = _("Row %(row_num)d : %(error_msg)s.") % {'row_num': row_num + 1, 'error_msg': str(e)}
        if not error and import_counter == 0:
            error = True
            error_msg = "{}".format(_("Nothing to import."))
        if error:
            transaction.savepoint_rollback(sid)
        else:
            transaction.savepoint_commit(sid)
    return error, error_msg
Esempio n. 4
0
def import_invoice_sheet(worksheet, invoice_reference=EMPTY_STRING,
                         customer_2_id_dict=None,
                         producer=None
                         ):
    error = False
    error_msg = None
    header = get_header(worksheet)
    if header:
        now = timezone.now().date()
        lut_reverse_vat = dict(LUT_ALL_VAT_REVERSE)
        import_counter = 0
        row_num = 1
        sid = transaction.savepoint()
        try:

            permanence = Permanence.objects.create(
                permanence_date=now,
                short_name=invoice_reference,
                status=PERMANENCE_SEND,
                highest_status=PERMANENCE_SEND
            )
            permanence.producers.add(producer)
            row = get_row(worksheet, header, row_num)
            while row and not error:
                customer_name = row[_("Customer")]
                if customer_name:
                    if customer_name in customer_2_id_dict:
                        customer_id = customer_2_id_dict[customer_name]
                    else:
                        error = True
                        error_msg = _("Row %(row_num)d : No valid customer") % {'row_num': row_num + 1}
                        break
                    product_reference = row[_("Reference")] or EMPTY_STRING
                    unit = row[_("Unit")]
                    order_unit = get_reverse_invoice_unit(unit)
                    vat = row[_("VAT level")]
                    vat_level = lut_reverse_vat[vat]
                    product = Product.objects.filter(producer_id=producer.id, reference=product_reference).order_by(
                        '?').first()
                    if product is None:
                        product = Product.objects.create(
                            producer=producer,
                            reference=product_reference,
                        )
                    long_name = row[_("Product")]
                    # The producer unit price is the imported customer unit price
                    # If the group get a reduction, this one must be mentioned into the producer admin screen
                    # into the "price_list_multiplier" field
                    product.producer_unit_price = row[_("Customer unit price")]
                    product.unit_deposit = row[_("Deposit")]
                    product.order_unit = order_unit
                    product.vat_level = vat_level
                    product.wrapped = row[_("Wrapped")]
                    qty_and_price_display = product.get_qty_and_price_display(customer_price=False)
                    if long_name.endswith(qty_and_price_display):
                        long_name = long_name[:-len(qty_and_price_display)]
                    product.long_name = long_name[:100]
                    product.save()
                    offer_item = product.get_or_create_offer_item(permanence, reset_add_2_stock=True)
                    create_or_update_one_purchase(
                        customer_id=customer_id,
                        offer_item=offer_item,
                        status=PERMANENCE_SEND,
                        q_order=Decimal(row[_("Quantity")]),
                        batch_job=True, is_box_content=False,
                        comment=row[_("Comment")] or EMPTY_STRING
                    )
                    import_counter += 1

                row_num += 1
                row = get_row(worksheet, header, row_num)
            reorder_offer_items(permanence.id)
            reorder_purchases(permanence.id)

        except KeyError as e:
            # Missing field
            error = True
            error_msg = _("Row %(row_num)d : A required column is missing %(error_msg)s.") % {
                'row_num': row_num + 1, 'error_msg': str(e)}
        except Exception as e:
            error = True
            error_msg = _("Row %(row_num)d : %(error_msg)s.") % {'row_num': row_num + 1, 'error_msg': str(e)}
        if not error and import_counter == 0:
            error = True
            error_msg = "{}".format(_("Nothing to import."))
        if error:
            transaction.savepoint_rollback(sid)
        else:
            transaction.savepoint_commit(sid)
    return error, error_msg