예제 #1
0
def close_and_send_order(permanence_id, everything=True, deliveries_id=()):
    # Be careful : use permanece_id, deliveries_id, ... and not objects
    # for the "thread" processing

    permanence = Permanence.objects.filter(id=permanence_id, status=PERMANENCE_OPENED).order_by('?').first()
    if permanence is None:
        return
    if permanence.with_delivery_point:
        if len(deliveries_id) == 0:
            return

    permanence.set_status(old_status=(PERMANENCE_OPENED,), new_status=PERMANENCE_WAIT_FOR_CLOSED, everything=everything,
                          deliveries_id=deliveries_id)
    permanence.close_order(everything=everything,
                           deliveries_id=deliveries_id)
    permanence.set_status(old_status=(PERMANENCE_WAIT_FOR_CLOSED,), new_status=PERMANENCE_CLOSED, everything=everything,
                          deliveries_id=deliveries_id)
    permanence.set_status(old_status=(PERMANENCE_CLOSED,), new_status=PERMANENCE_WAIT_FOR_SEND, everything=everything,
                          deliveries_id=deliveries_id)
    permanence.recalculate_order_amount(send_to_producer=True)
    reorder_purchases(permanence.id)
    email_order.email_order(permanence.id, everything=everything,
                            deliveries_id=deliveries_id)
    permanence.set_status(old_status=(PERMANENCE_WAIT_FOR_SEND,), new_status=PERMANENCE_SEND, everything=everything,
                          deliveries_id=deliveries_id)
예제 #2
0
def close_and_send_order(permanence_id,
                         everything=True,
                         producers_id=(),
                         deliveries_id=()):
    # Be careful : use permanece_id, deliveries_id, ... and not objects
    # for the "thread" processing

    permanence = Permanence.objects.filter(
        id=permanence_id, status=PERMANENCE_OPENED).order_by('?').first()
    if permanence is None:
        return
    if permanence.with_delivery_point:
        if len(deliveries_id) == 0:
            return
        if len(producers_id) > 0:
            return
    else:
        if len(deliveries_id) > 0:
            return
        if not everything:
            if len(producers_id) == 0:
                return

            if settings.REPANIER_SETTINGS_CUSTOMER_MUST_CONFIRM_ORDER:
                return

    permanence.set_status(old_status=(PERMANENCE_OPENED, ),
                          new_status=PERMANENCE_WAIT_FOR_CLOSED,
                          everything=everything,
                          producers_id=producers_id,
                          deliveries_id=deliveries_id)
    permanence.close_order(everything=everything,
                           producers_id=producers_id,
                           deliveries_id=deliveries_id)
    permanence.set_status(old_status=(PERMANENCE_WAIT_FOR_CLOSED, ),
                          new_status=PERMANENCE_CLOSED,
                          everything=everything,
                          producers_id=producers_id,
                          deliveries_id=deliveries_id)
    permanence.set_status(old_status=(PERMANENCE_CLOSED, ),
                          new_status=PERMANENCE_WAIT_FOR_SEND,
                          everything=everything,
                          producers_id=producers_id,
                          deliveries_id=deliveries_id)
    permanence.recalculate_order_amount(send_to_producer=True)
    reorder_purchases(permanence.id)
    email_order.email_order(permanence.id,
                            everything=everything,
                            producers_id=producers_id,
                            deliveries_id=deliveries_id)
    permanence.set_status(old_status=(PERMANENCE_WAIT_FOR_SEND, ),
                          new_status=PERMANENCE_SEND,
                          everything=everything,
                          producers_id=producers_id,
                          deliveries_id=deliveries_id)
예제 #3
0
def send_order(permanence,
               all_producers=True,
               producers_id=None,
               deliveries_id=None):
    permanence.recalculate_order_amount(send_to_producer=True)
    reorder_purchases(permanence.id)
    # try:
    email_order.email_order(permanence.id,
                            all_producers,
                            producers_id=producers_id,
                            closed_deliveries_id=deliveries_id)
예제 #4
0
def close_order(permanence_id,
                everything=True,
                deliveries_id=(),
                send_mail=True):
    # Be careful : use permanece_id, deliveries_id, ... and not objects
    # for the "thread" processing

    permanence = (Permanence.objects.filter(
        id=permanence_id, status=PERMANENCE_OPENED).order_by("?").first())
    if permanence is None:
        return
    if permanence.with_delivery_point:
        if len(deliveries_id) == 0:
            return

    permanence.set_status(
        old_status=PERMANENCE_OPENED,
        new_status=PERMANENCE_WAIT_FOR_CLOSED,
        everything=everything,
        deliveries_id=deliveries_id,
    )
    permanence.close_order(everything=everything,
                           deliveries_id=deliveries_id,
                           send_mail=send_mail)
    permanence.set_status(
        old_status=PERMANENCE_WAIT_FOR_CLOSED,
        new_status=PERMANENCE_CLOSED,
        everything=everything,
        deliveries_id=deliveries_id,
    )
    permanence.set_status(
        old_status=PERMANENCE_CLOSED,
        new_status=PERMANENCE_WAIT_FOR_SEND,
        everything=everything,
        deliveries_id=deliveries_id,
    )
    permanence.recalculate_order_amount(send_to_producer=True)
    reorder_purchases(permanence.id)
    if send_mail:
        email_order.email_order(permanence.id,
                                everything=everything,
                                deliveries_id=deliveries_id)
    permanence.set_status(
        old_status=PERMANENCE_WAIT_FOR_SEND,
        new_status=PERMANENCE_SEND,
        everything=everything,
        deliveries_id=deliveries_id,
    )
예제 #5
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
예제 #6
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