def send_order(permanence, all_producers, producers_id=None, deliveries_id=None): getcontext().rounding = ROUND_HALF_UP recalculate_order_amount( permanence_id=permanence.id, all_producers=all_producers, producers_id=producers_id, send_to_producer=True ) try: email_order.email_order(permanence.id, all_producers, closed_deliveries_id=deliveries_id, producers_id=producers_id) except Exception as error_str: print("################################## send_order") print(error_str) print("##################################")
def common_to_pre_open_and_open(permanence_id): getcontext().rounding = ROUND_HALF_UP # 1- Deactivate all offer item of this permanence OfferItem.objects.filter( permanence_id=permanence_id ).order_by('?').update( is_active=False, may_order=False, is_box=False, is_box_content=False ) # 2 - Delete unused purchases Purchase.objects.filter(permanence_id=permanence_id, quantity_ordered=0, quantity_invoiced=0).order_by('?').delete() # 3 - Activate offer items which can be purchased depending on selection in the admin producers_in_this_permanence = Producer.objects.filter( permanence=permanence_id, is_active=True).order_by('?').only("id") product_queryset = Product.objects.filter( producer__in=producers_in_this_permanence, is_into_offer=True ).order_by('?').only("id", "producer_id") OfferItem.objects.filter(product__in=product_queryset, permanence_id=permanence_id) \ .order_by('?').update(is_active=True, may_order=True) for product in product_queryset: if not OfferItem.objects.filter(product_id=product.id, permanence_id=permanence_id) \ .order_by('?').exists(): OfferItem.objects.create( permanence_id=permanence_id, product_id=product.id, producer_id=product.producer_id, is_box=False, is_box_content=False, is_active=True, may_order=True ) # 4 - Add composition products product_queryset = Product.objects.filter( is_box=True, is_into_offer=True ).order_by('?').only("id", "producer_id") OfferItem.objects.filter(product__in=product_queryset, permanence_id=permanence_id) \ .order_by('?').update(is_active=True, may_order=True) for product in product_queryset: offer_item = OfferItem.objects.filter(product_id=product.id, permanence_id=permanence_id).order_by('?').first() if offer_item is None: OfferItem.objects.create( permanence_id=permanence_id, product_id=product.id, producer_id=product.producer_id, is_box=True, is_box_content=False, is_active=True, may_order=True ) else: offer_item.is_box = True offer_item.is_active = True offer_item.save(update_fields=["is_active", "is_box"]) for box_content in BoxContent.objects.filter(box=product.id).select_related("product__producer").order_by( '?'): box_offer_item = OfferItem.objects.filter(product_id=box_content.product_id, permanence_id=permanence_id).order_by('?').first() if box_offer_item is None: OfferItem.objects.create( permanence_id=permanence_id, product_id=box_content.product_id, producer_id=box_content.product.producer_id, is_box=False, is_box_content=True, is_active=True, may_order=False ) else: box_offer_item.is_box_content = True box_offer_item.is_active = True box_offer_item.save(update_fields=["is_active", "is_box_content"]) # 6 - Add subscriptions even if the producer is not selected product_queryset = Product.objects.filter( order_unit=PRODUCT_ORDER_UNIT_SUBSCRIPTION, is_into_offer=True, producer__represent_this_buyinggroup=True ).order_by('?').only("id", "producer_id") for product in product_queryset: if not OfferItem.objects.filter(product_id=product.id, permanence_id=permanence_id) \ .order_by('?').exists(): OfferItem.objects.create( permanence_id=permanence_id, product_id=product.id, producer_id=product.producer_id, is_box=False, is_box_content=False, ) # 7 - Activate purchased products even if not in selected in the admin OfferItem.objects.filter( purchase__permanence_id=permanence_id, is_active=False, order_unit__lt=PRODUCT_ORDER_UNIT_DEPOSIT ).order_by('?').update(is_active=True) # 8 - Create cache permanence = Permanence.objects.filter(id=permanence_id).order_by('?').first() offer_item_qs = OfferItem.objects.filter(permanence_id=permanence_id).order_by('?') clean_offer_item(permanence, offer_item_qs, reset_add_2_stock=True) # 9 - calculate the sort order of the order display screen reorder_offer_items(permanence_id) # 10 - Deactivate technical offer items OfferItem.objects.filter(order_unit__gte=PRODUCT_ORDER_UNIT_DEPOSIT, permanence_id=permanence_id) \ .order_by('?').update(is_active=False, may_order=False) # 11 - Calculate the Purchase 'sum' for each customer recalculate_order_amount( permanence_id=permanence_id, all_producers=True, send_to_producer=False ) return permanence
def close_order(permanence, all_producers, producers_id=None): getcontext().rounding = ROUND_HALF_UP today = timezone.now().date() if repanier.apps.REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS: # 0 - Cancel unconfirmed purchases purchase_qs = Purchase.objects.filter( permanence_id=permanence.id, customer_invoice__is_order_confirm_send=False, is_box_content=False ).exclude( quantity_ordered=DECIMAL_ZERO ).order_by('customer_invoice') if not all_producers: purchase_qs = purchase_qs.filter(producer_id__in=producers_id) customer_invoice_id_save = -1 for purchase in purchase_qs.select_related("customer", "offer_item"): if customer_invoice_id_save != purchase.customer_invoice_id: customer_invoice_id_save = purchase.customer_invoice_id # This order has been cancelled # filename = force_filename("%s - %s.xlsx" % (_("Canceled order"), permanence)) filename = "{0}-{1}.xlsx".format( slugify(_("Canceled order")), slugify(permanence) ) sender_email, sender_function, signature, cc_email_staff = get_signature( is_reply_to_order_email=True) export_order_2_1_customer( purchase.customer, filename, permanence, sender_email, sender_function, signature, cancel_order=True) update_or_create_purchase( customer=purchase.customer, offer_item_id=purchase.offer_item.id, value_id=DECIMAL_ZERO, batch_job=True ) else: # 0 - Delete unused purchases purchase_qs = Purchase.objects.filter( permanence_id=permanence.id, quantity_ordered=0 ).order_by('?') if not all_producers: purchase_qs = purchase_qs.filter(producer_id__in=producers_id) purchase_qs.delete() # 1 - Round to multiple producer_order_by_quantity offer_item_qs = OfferItem.objects.filter( permanence_id=permanence.id, is_active=True, order_unit__lt=PRODUCT_ORDER_UNIT_DEPOSIT, producer_order_by_quantity__gt=1, quantity_invoiced__gt=0 ).order_by('?') if not all_producers: offer_item_qs = offer_item_qs.filter(producer_id__in=producers_id) for offer_item in offer_item_qs: # It's possible to round the ordered qty even If we do not manage stock if offer_item.manage_replenishment: needed = (offer_item.quantity_invoiced - offer_item.stock) else: needed = offer_item.quantity_invoiced if needed > DECIMAL_ZERO: offer_item.add_2_stock = offer_item.producer_order_by_quantity - ( needed % offer_item.producer_order_by_quantity) offer_item.save() # 2 - Add Transport offer_item_qs = OfferItem.objects.filter( permanence_id=permanence.id, is_active=False, order_unit=PRODUCT_ORDER_UNIT_TRANSPORTATION ).order_by('?') if not all_producers: offer_item_qs = offer_item_qs.filter(producer_id__in=producers_id) for offer_item in offer_item_qs: buying_group = Customer.objects.filter(is_active=True, represent_this_buyinggroup=True).order_by('?').first() create_or_update_one_purchase(buying_group, offer_item, 1, None, True, is_box_content=False) membership_fee_product = Product.objects.filter(is_membership_fee=True, is_active=True).order_by('?').first() membership_fee_product.producer_unit_price = repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE # Update the prices membership_fee_product.save() membership_fee_offer_item = get_or_create_offer_item( permanence, membership_fee_product.id, membership_fee_product.producer_id ) for customer in Customer.objects.filter( is_active=True, may_order=True, customerinvoice__permanence_id=permanence.id, customerinvoice__total_price_with_tax__gt=DECIMAL_ZERO, represent_this_buyinggroup=False ).order_by('?'): # 3 - Add Deposit offer_item_qs = OfferItem.objects.filter( permanence_id=permanence.id, order_unit=PRODUCT_ORDER_UNIT_DEPOSIT ).order_by('?') if not all_producers: offer_item_qs = offer_item_qs.filter(producer_id__in=producers_id) for offer_item in offer_item_qs: create_or_update_one_purchase(customer, offer_item, 1, None, True, is_box_content=False) create_or_update_one_purchase(customer, offer_item, 0, None, True, is_box_content=False) # 4 - Add Add Membership fee Subscription if repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE_DURATION > 0: # There is a membership fee if customer.membership_fee_valid_until < today: permanence.producers.add(membership_fee_offer_item.producer_id) create_or_update_one_purchase(customer, membership_fee_offer_item, 1, None, True, is_box_content=False) while customer.membership_fee_valid_until < today: # Do not pay the membership fee if no order passed during a certain amount of time customer.membership_fee_valid_until = add_months( customer.membership_fee_valid_until, repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE_DURATION ) customer.save(update_fields=['membership_fee_valid_until', ]) # 5 - Add Common participation Subscription if all_producers: for offer_item in OfferItem.objects.filter( permanence_id=permanence.id, is_membership_fee=False, order_unit=PRODUCT_ORDER_UNIT_SUBSCRIPTION ).order_by('?'): for customer in Customer.objects.filter( is_active=True, may_order=True, represent_this_buyinggroup=False ).order_by('?'): permanence.producers.add(offer_item.producer_id) create_or_update_one_purchase(customer, offer_item, 1, None, True, is_box_content=False) # Disable subscription for next permanence Product.objects.filter( order_unit=PRODUCT_ORDER_UNIT_SUBSCRIPTION, is_into_offer=True, is_membership_fee=False ).order_by('?').update(is_into_offer=False) permanence.set_status(PERMANENCE_CLOSED, allow_downgrade=False) if not repanier.apps.REPANIER_SETTINGS_INVOICE and repanier.apps.REPANIER_SETTINGS_BANK_ACCOUNT is None: # No Invoice and no bank_account --> auto archive # Put send permanences to the done status, because they will "never" be invoiced for permanence in Permanence.objects.filter(status=PERMANENCE_SEND): permanence.set_status(PERMANENCE_ARCHIVED, update_payment_date=True) else: permanence.set_status(PERMANENCE_CLOSED, all_producers=all_producers, producers_id=producers_id) # 6 - Refresh the Purchase 'sum' for each customer recalculate_order_amount( permanence_id=permanence.id, all_producers=all_producers, producers_id=producers_id, send_to_producer=False, )
def close_order_delivery(permanence, delivery, all_producers, producers_id=None): today = timezone.now().date() getcontext().rounding = ROUND_HALF_UP # 0 - Delete unused purchases # No need to select : customer_invoice__delivery = delivery Purchase.objects.filter( permanence_id=permanence.id, quantity_ordered=0 ).order_by('?').delete() if repanier.apps.REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS: purchase_qs = Purchase.objects.filter( permanence_id=permanence.id, customer_invoice__delivery=delivery, customer_invoice__is_order_confirm_send=False, is_box_content=False ).exclude( quantity_ordered=DECIMAL_ZERO ).order_by('customer_invoice') if not all_producers: # This may never be the but, but... purchase_qs = purchase_qs.filter(producer_id__in=producers_id) customer_invoice_id_save = -1 for purchase in purchase_qs.select_related("customer", "offer_item"): if customer_invoice_id_save != purchase.customer_invoice_id: customer_invoice_id_save =purchase.customer_invoice_id # This order has been cancelled # filename = force_filename("%s - %s.xlsx" % (_("Canceled order"), permanence)) filename = "{0}-{1}.xlsx".format( slugify(_("Canceled order")), slugify(permanence) ) sender_email, sender_function, signature, cc_email_staff = get_signature( is_reply_to_order_email=True) export_order_2_1_customer( purchase.customer, filename, permanence, sender_email, sender_function, signature, cancel_order=True) update_or_create_purchase( customer=purchase.customer, offer_item_id=purchase.offer_item.id, value_id=DECIMAL_ZERO, batch_job=True ) if all_producers: # 1 - Do not round to multiple producer_order_by_quantity # 2 - Do not add Transport membership_fee_product = Product.objects.filter(is_membership_fee=True, is_active=True).order_by('?').first() membership_fee_product.producer_unit_price = repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE # Update the prices membership_fee_product.save() membership_fee_offer_item = get_or_create_offer_item(permanence, membership_fee_product.id, membership_fee_product.producer_id) for customer in Customer.objects.filter( is_active=True, may_order=True, customerinvoice__permanence_id=permanence.id, customerinvoice__delivery=delivery, customerinvoice__total_price_with_tax__gt=DECIMAL_ZERO, represent_this_buyinggroup=False ).order_by('?'): # 3 - Add Deposit for offer_item in OfferItem.objects.filter(permanence_id=permanence.id, # is_active=False, order_unit=PRODUCT_ORDER_UNIT_DEPOSIT).order_by('?'): permanence.producers.add(offer_item.producer_id) create_or_update_one_purchase(customer, offer_item, 1, None, True, is_box_content=False) create_or_update_one_purchase(customer, offer_item, 0, None, True, is_box_content=False) # 4 - Add Membership fee Subscription if repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE_DURATION > 0: # There is a membership fee if customer.membership_fee_valid_until < today: permanence.producers.add(membership_fee_offer_item.producer_id) create_or_update_one_purchase(customer, membership_fee_offer_item, 1, None, True, is_box_content=False) customer.membership_fee_valid_until = add_months( customer.membership_fee_valid_until, repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE_DURATION ) customer.save(update_fields=['membership_fee_valid_until', ]) # 5 - Add Common participation Subscription for offer_item in OfferItem.objects.filter( permanence_id=permanence.id, is_membership_fee=False, order_unit=PRODUCT_ORDER_UNIT_SUBSCRIPTION).order_by('?'): for customer in Customer.objects.filter(is_active=True, may_order=True, represent_this_buyinggroup=False).order_by('?'): permanence.producers.add(offer_item.producer_id) create_or_update_one_purchase(customer, offer_item, 1, None, True, is_box_content=False) # 6 - Refresh the Purchase 'sum' recalculate_order_amount( permanence_id=permanence.id, all_producers=all_producers, producers_id=producers_id, send_to_producer=False ) delivery.set_status(PERMANENCE_CLOSED, all_producers, producers_id)
def import_purchase_sheet(worksheet, permanence=None, customer_2_id_dict=None, producer_2_id_dict=None ): error = False error_msg = None import_counter = 0 header = get_header(worksheet) if header: row_num = 1 array_purchase = [] rule_of_3_source = DECIMAL_ZERO row = get_row(worksheet, header, row_num) while row and not error: try: row_format = row[_('Format')] if row_format in ["A", "B"]: import_counter += 1 if row[_('Id')] is None: error = True error_msg = _("Row %(row_num)d : No purchase id given.") % {'row_num': row_num + 1} break row_id = Decimal(row[_('Id')]) purchase = Purchase.objects.filter(id=row_id).order_by('?').first() if purchase is None: error = True error_msg = _("Row %(row_num)d : No purchase corresponding to the given purchase id.") % { 'row_num': row_num + 1} break if purchase.permanence_id != permanence.id: error = True error_msg = _("Row %(row_num)d : The given permanence doesn't own the given purchase id.") % { 'row_num': row_num + 1} break producer_id = None if row[_('producer')] in producer_2_id_dict: producer_id = producer_2_id_dict[row[_('producer')]] if producer_id != purchase.producer_id: error = True error_msg = _("Row %(row_num)d : No valid producer.") % {'row_num': row_num + 1} break customer_name = "%s" % row[_('customer')] if customer_name in customer_2_id_dict: customer_id = customer_2_id_dict[customer_name] if customer_id != purchase.customer_id: error = True error_msg = _("Row %(row_num)d : No valid customer") % {'row_num': row_num + 1} break comment = cap(row[_('comment')], 100) quantity_invoiced = DECIMAL_ZERO if row[_('quantity invoiced')] is None \ else Decimal(row[_('quantity invoiced')]).quantize(FOUR_DECIMALS) producer_row_price = row[_('purchase price')] if producer_row_price is not None: producer_row_price = Decimal(producer_row_price).quantize(TWO_DECIMALS) if purchase.purchase_price.amount != producer_row_price: purchase_price_modified = True if purchase.offer_item.order_unit in [ PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG, PRODUCT_ORDER_UNIT_LT ]: producer_unit_price = (purchase.offer_item.producer_unit_price.amount + purchase.offer_item.unit_deposit.amount).quantize(TWO_DECIMALS) if producer_unit_price != DECIMAL_ZERO: purchase.quantity_invoiced = (producer_row_price / producer_unit_price).quantize(FOUR_DECIMALS) else: purchase.quantity_invoiced = DECIMAL_ZERO else: purchase_price_modified = False elif purchase.quantity_invoiced != quantity_invoiced: purchase.quantity_invoiced = quantity_invoiced purchase_price_modified = False else: purchase_price_modified = False if row_format == "A": array_purchase = [] rule_of_3_source = DECIMAL_ZERO if not purchase_price_modified: producer_unit_price = Decimal(row[_('producer unit price')]).quantize(TWO_DECIMALS) previous_producer_unit_price = purchase.get_producer_unit_price() if producer_unit_price != previous_producer_unit_price: offer_item = OfferItem.objects.filter(id=purchase.offer_item_id).order_by('?').first() offer_item.producer_unit_price = producer_unit_price recalculate_prices(offer_item, offer_item.producer_price_are_wo_vat, offer_item.is_resale_price_fixed, offer_item.price_list_multiplier) offer_item.save() recalculate_order_amount( permanence_id=offer_item.permanence_id, offer_item_queryset=OfferItem.objects.filter(id=offer_item.id).order_by('?'), send_to_producer=False ) purchase.comment = comment purchase.save() rule_of_3_source += purchase.purchase_price.amount array_purchase.append(purchase) elif row_format in ["C", "D"]: rule_of_3_target = row[_('rule of 3')] if rule_of_3_target is not None: rule_of_3_target = Decimal(rule_of_3_target).quantize(TWO_DECIMALS) if rule_of_3_target != rule_of_3_source: max_purchase_counter = len(array_purchase) if max_purchase_counter <= 1: error = True error_msg = _("Row %(row_num)d : Rule of 3 target in wrong context.") % { 'row_num': row_num + 1} return error, error_msg else: if rule_of_3_source != DECIMAL_ZERO: ratio = rule_of_3_target / rule_of_3_source else: if rule_of_3_target == DECIMAL_ZERO: ratio = DECIMAL_ZERO else: ratio = DECIMAL_ONE # Rule of 3 if ratio != DECIMAL_ONE: adjusted_invoice = DECIMAL_ZERO for i, purchase in enumerate(array_purchase, start=1): producer_unit_price = (purchase.offer_item.producer_unit_price.amount + purchase.offer_item.unit_deposit.amount ).quantize(TWO_DECIMALS) if i == max_purchase_counter: delta = rule_of_3_target - adjusted_invoice if producer_unit_price != DECIMAL_ZERO: purchase.quantity_invoiced = (delta / producer_unit_price).quantize( FOUR_DECIMALS) else: purchase.quantity_invoiced = DECIMAL_ZERO else: purchase.quantity_invoiced = (purchase.quantity_invoiced * ratio).quantize( FOUR_DECIMALS) adjusted_invoice += purchase.quantity_invoiced * producer_unit_price purchase.save() 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 %(error_msg)s.") % { 'row_num': row_num + 1, 'error_msg': str(e)} except Exception, e: error = True error_msg = _("Row %(row_num)d : %(error_msg)s.") % {'row_num': row_num + 1, 'error_msg': str(e)}
def handle(self, *args, **options): translation.activate(settings.LANGUAGE_CODE) # Les producer price (et donc la tva) des produits, offer item dont is_box = true est à zéro au lieu d'être = au prix, eva consommateur Product.objects.filter( is_box=True ).update( producer_unit_price=F('customer_unit_price'), producer_vat=F('customer_vat') ) Product.objects.filter( vat_level='200' ).update( vat_level='100' ) Product.objects.filter( vat_level='300' ).update( vat_level='100' ) OfferItem.objects.filter( is_box=True ).update( producer_unit_price=F('customer_unit_price'), producer_vat=F('customer_vat') ) OfferItem.objects.filter( vat_level='200' ).update( vat_level='100' ) OfferItem.objects.filter( vat_level='300' ).update( vat_level='100' ) Purchase.objects.filter( offer_item__is_box=True ).update( purchase_price=F('selling_price'), producer_vat=F('customer_vat') ) # Mettre purchase price et selling price des purchase dont is_box_content = True à zéro Purchase.objects.filter( is_box_content=True ).update( purchase_price=DECIMAL_ZERO, selling_price=DECIMAL_ZERO, producer_vat=DECIMAL_ZERO, customer_vat=DECIMAL_ZERO, # compensation=DECIMAL_ZERO, deposit=DECIMAL_ZERO, ) Purchase.objects.filter( vat_level='200' ).update( vat_level='100' ) Purchase.objects.filter( vat_level='300' ).update( vat_level='100' ) qs = CustomerInvoice.objects.filter( permanence__with_delivery_point=True, status__lte=PERMANENCE_CLOSED, delivery__isnull=True ).order_by('?') Purchase.objects.filter( customer_invoice__in=qs ).order_by('?').delete() qs.delete() OfferItem.objects.filter( producer_price_are_wo_vat=True ).update( producer_price_are_wo_vat=False, producer_unit_price=F('producer_unit_price') + F('producer_vat') ) OfferItem.objects.filter( order_unit__gte=PRODUCT_ORDER_UNIT_DEPOSIT ).update( manage_replenishment=False ) for permanence in Permanence.objects.filter( with_delivery_point=False ).order_by('?'): CustomerInvoice.objects.filter( permanence_id=permanence.id, ).order_by('?').update( customer_who_pays_id=F('customer_id') ) Purchase.objects.filter( permanence_id=permanence.id, ).order_by('?').update( customer_who_pays_id=F('customer_id') ) for producer_invoice in ProducerInvoice.objects.filter( invoice_reference__isnull=True ): bank_account = BankAccount.objects.filter( producer_invoice_id = producer_invoice.id, operation_status=BANK_CALCULATED_INVOICE ).order_by('?').first() if bank_account is not None: producer_invoice.invoice_reference = bank_account.operation_comment producer_invoice.to_be_invoiced_balance = bank_account.bank_amount_out - bank_account.bank_amount_in producer_invoice.save(update_fields=['invoice_reference', 'to_be_invoiced_balance']) for permanence in Permanence.objects.filter( status__lt=PERMANENCE_CLOSED ).order_by('permanence_date'): print ("%s %s" % (permanence.permanence_date, permanence.get_status_display())) recalculate_order_amount( permanence_id=permanence.id, all_producers=True, send_to_producer=False, re_init=True) reorder_offer_items(permanence.id) for customer_invoice in CustomerInvoice.objects.filter(permanence_id=permanence.id): delivery_point = LUT_DeliveryPoint.objects.filter( customer_responsible=customer_invoice.customer_id ).order_by('?').first() if delivery_point is not None: delivery = DeliveryBoard.objects.filter( delivery_point_id=delivery_point.id, permanence_id=permanence.id, ).order_by('?').first() customer_invoice.delivery = delivery customer_invoice.set_delivery(customer_invoice.delivery) if customer_invoice.is_order_confirm_send: customer_invoice.confirm_order() customer_invoice.save() # if customer_invoice.is_order_confirm_send: # confirm_customer_invoice(permanence.id, customer_invoice.customer_id) for permanence in Permanence.objects.filter( status__gte=PERMANENCE_CLOSED ).order_by('permanence_date'): print ("%s %s" % (permanence.permanence_date, permanence.get_status_display())) status = permanence.status permanence.set_status(status) if status == PERMANENCE_SEND: recalculate_order_amount( permanence_id=permanence.id, all_producers=True, send_to_producer=False, re_init=True ) reorder_offer_items(permanence.id) for customer_invoice in CustomerInvoice.objects.filter(permanence_id=permanence.id): delivery_point = LUT_DeliveryPoint.objects.filter( customer_responsible=customer_invoice.customer_id ).order_by('?').first() if delivery_point is not None: print("---- %s" % delivery_point) delivery = DeliveryBoard.objects.filter( delivery_point_id=delivery_point.id, permanence_id=permanence.id, ).order_by('?').first() customer_invoice.delivery = delivery customer_invoice.set_delivery(customer_invoice.delivery) if customer_invoice.is_order_confirm_send: customer_invoice.confirm_order() customer_invoice.save()