class PurchaseResource(resources.ModelResource): id = fields.Field(attribute='id', widget=IdWidget(), readonly=True) short_name = fields.Field(attribute='permanence__short_name', readonly=True) date = fields.Field(attribute='permanence__permanence_date', widget=DateWidgetExcel(), readonly=True) product_id = fields.Field(attribute='offer_item__product__id', readonly=True) product = fields.Field(attribute='offer_item__get_long_name', readonly=True) producer_id = fields.Field(attribute='producer__id', readonly=True) producer_name = fields.Field(attribute='producer__short_profile_name', readonly=True) customer_id = fields.Field(attribute='customer__id', readonly=True) customer_name = fields.Field(attribute='customer__short_basket_name', readonly=True) unit_deposit = fields.Field(attribute='offer_item__unit_deposit', widget=TwoMoneysWidget(), readonly=True) producer_unit_price_wo_tax = fields.Field(attribute='offer_item__producer_unit_price_wo_tax', widget=TwoMoneysWidget(), readonly=True) producer_vat = fields.Field(attribute='offer_item__producer_vat', widget=TwoMoneysWidget(), readonly=True) customer_unit_price = fields.Field(attribute='offer_item__customer_unit_price', widget=TwoMoneysWidget(), readonly=True) customer_vat = fields.Field(attribute='offer_item__customer_vat', widget=TwoMoneysWidget(), readonly=True) quantity_invoiced = fields.Field(attribute='quantity_invoiced', widget=FourDecimalsWidget(), readonly=True) producer_row_price = fields.Field(attribute='purchase_price', widget=TwoMoneysWidget(), readonly=True) customer_row_price = fields.Field(attribute='selling_price', widget=TwoMoneysWidget(), readonly=True) vat_level = fields.Field(attribute='offer_item__vat_level', widget=ChoiceWidget(LUT_ALL_VAT, LUT_ALL_VAT_REVERSE), readonly=True) class Meta: model = Purchase fields = ( 'id', 'date', 'short_name', 'producer_id', 'producer_name', 'product_id', 'product', 'customer_id', 'customer_name', 'quantity_invoiced', 'unit_deposit', 'producer_unit_price_wo_tax', 'producer_vat', 'customer_unit_price', 'customer_vat', 'producer_row_price', 'customer_row_price', 'vat_level', 'comment' ) export_order = fields import_id_fields = ('id',) skip_unchanged = True report_skipped = False use_transactions = False
class BankAccountResource(resources.ModelResource): id = fields.Field(attribute='id', widget=IdWidget(), readonly=True) permanence = fields.Field(attribute='permanence', readonly=True) operation_date = fields.Field(attribute='operation_date', widget=DateWidgetExcel()) operation_status = fields.Field(attribute='operation_status', readonly=True) producer_name = fields.Field(attribute='producer', widget=ProducerNameWidget(Producer, field='short_profile_name')) customer_name = fields.Field(attribute='customer', widget=CustomerNameWidget(Customer, field='short_basket_name')) bank_amount_in = fields.Field(attribute='bank_amount_in', widget=TwoMoneysWidget()) bank_amount_out = fields.Field(attribute='bank_amount_out', widget=TwoMoneysWidget()) customer_invoice = fields.Field(attribute='customer_invoice__date_balance', widget=DateWidgetExcel(), readonly=True) producer_invoice = fields.Field(attribute='producer_invoice__date_balance', widget=DateWidgetExcel(), readonly=True) def before_save_instance(self, instance, using_transactions, dry_run): """ Override to add additional logic. """ if instance.id is None: if instance.producer is None and instance.customer is None: if instance.bank_amount_out is not None and instance.bank_amount_out != DECIMAL_ZERO: only_one_target = ProducerInvoice.objects.filter( status=PERMANENCE_SEND, total_price_with_tax=instance.bank_amount_out ).order_by('?').count() if only_one_target == 1: instance.producer = ProducerInvoice.objects.filter( status=PERMANENCE_SEND, total_price_with_tax=instance.bank_amount_out ).order_by('?').first().producer elif instance.bank_amount_in is not None and instance.bank_amount_in != DECIMAL_ZERO: only_one_target = CustomerInvoice.objects.filter( status=PERMANENCE_SEND, total_price_with_tax=instance.bank_amount_in ).order_by('?').count() if only_one_target == 1: instance.customer = CustomerInvoice.objects.filter( status=PERMANENCE_SEND, total_price_with_tax=instance.bank_amount_in ).order_by('?').first().customer if instance.producer is None and instance.customer is None: raise ValueError(_("No producer nor customer found.")) if instance.bank_amount_out is None: instance.bank_amount_out = DECIMAL_ZERO if instance.bank_amount_in is None: instance.bank_amount_in = DECIMAL_ZERO if instance.producer is not None and instance.customer is not None: raise ValueError(_("Only a customer or a producer may be entered.")) if instance.producer is not None: if BankAccount.objects.filter( producer=instance.producer, bank_amount_in=instance.bank_amount_in, bank_amount_out=instance.bank_amount_out, operation_date=instance.operation_date ).exists(): raise ValueError(_("This movement already exists.")) if BankAccount.objects.filter( producer=instance.producer, bank_amount_in=instance.bank_amount_in, bank_amount_out=instance.bank_amount_out, operation_comment=instance.operation_comment ).exists(): raise ValueError(_("This movement already exists.")) if instance.customer is not None: if BankAccount.objects.filter( customer=instance.customer, bank_amount_in=instance.bank_amount_in, bank_amount_out=instance.bank_amount_out, operation_date=instance.operation_date ).exists(): raise ValueError(_("This movement already exists.")) if BankAccount.objects.filter( customer=instance.customer, bank_amount_in=instance.bank_amount_in, bank_amount_out=instance.bank_amount_out, operation_comment=instance.operation_comment ).exists(): raise ValueError(_("This movement already exists.")) def skip_row(self, instance, original): if instance.id is not None: # The import may not be used to update bank movements. return True super(BankAccountResource, self).skip_row(instance, original) class Meta: model = BankAccount fields = ( 'id', 'permanence', 'operation_date', 'operation_status', 'producer_name', 'customer_name', 'bank_amount_in', 'bank_amount_out', 'operation_comment', 'customer_invoice', 'producer_invoice' ) export_order = fields import_id_fields = ('id', ) skip_unchanged = True report_skipped = False use_transactions = False
class CustomerResource(resources.ModelResource): id = fields.Field(attribute='id', widget=IdWidget(), readonly=True) email = fields.Field(attribute='user', default="*****@*****.**", widget=OneToOneWidget(User, 'email'), readonly=False) phone1 = fields.Field(attribute='phone1', default="1234", widget=CharWidget(), readonly=False) phone2 = fields.Field(attribute='phone2', widget=CharWidget(), readonly=False) date_balance = fields.Field(attribute='get_admin_date_balance', widget=CharWidget(), readonly=True) balance = fields.Field(attribute='get_admin_balance', widget=TwoMoneysWidget(), readonly=True) may_order = fields.Field(attribute='may_order', default=False, widget=DecimalBooleanWidget(), readonly=False) is_group = fields.Field(attribute='is_group', default=False, widget=DecimalBooleanWidget(), readonly=False) represent_this_buyinggroup = fields.Field( attribute='represent_this_buyinggroup', default=False, widget=DecimalBooleanWidget(), readonly=True) is_active = fields.Field(attribute='is_active', widget=DecimalBooleanWidget(), readonly=True) membership_fee_valid_until = fields.Field( attribute='membership_fee_valid_until', default=timezone.now().date(), widget=DateWidgetExcel(), readonly=False) last_membership_fee = fields.Field(attribute='get_last_membership_fee', widget=TwoMoneysWidget(), readonly=True) last_membership_fee_date = fields.Field( attribute='last_membership_fee_date', widget=DateWidgetExcel(), readonly=True) purchase = fields.Field(attribute='get_purchase', widget=ZeroDecimalsWidget(), readonly=True) participation = fields.Field(attribute='get_participation', widget=ZeroDecimalsWidget(), readonly=True) delivery_point = fields.Field(attribute='delivery_point', widget=TranslatedForeignKeyWidget( LUT_DeliveryPoint, field='short_name')) valid_email = fields.Field(attribute='valid_email', widget=DecimalBooleanWidget(), readonly=True) zero_waste = fields.Field(attribute='zero_waste', widget=DecimalBooleanWidget(), readonly=True) date_joined = fields.Field(attribute='get_admin_date_joined', widget=CharWidget(), readonly=True) def before_save_instance(self, instance, using_transactions, dry_run): """ Override to add additional logic. """ user_model = get_user_model() user_email_qs = user_model.objects.filter(email=instance.user.email, is_staff=False).order_by('?') user_username_qs = user_model.objects.filter( username=instance.short_basket_name).order_by('?') if instance.id is not None: customer = Customer.objects.filter( id=instance.id).order_by('?').only('id', 'user_id').first() user_email_qs = user_email_qs.exclude(id=customer.user_id) user_username_qs = user_username_qs.exclude(id=customer.user_id) else: customer = None if user_email_qs.exists(): raise ValueError( _("The email {} is already used by another user.").format( instance.user.email)) if user_username_qs.exists(): raise ValueError( _("The short_basket_name {} is already used by another user."). format(instance.short_basket_name)) if using_transactions or not dry_run: if instance.id is not None: email = instance.user.email instance.user = user_model.objects.get(id=customer.user_id) instance.user.username = instance.short_basket_name instance.user.first_name = EMPTY_STRING instance.user.last_name = instance.short_basket_name instance.user.email = email instance.user.save() else: instance.user = user_model.objects.create_user( username=instance.short_basket_name, email=instance.user.email, password=uuid.uuid1().hex, first_name=EMPTY_STRING, last_name=instance.short_basket_name) instance.user_id = instance.user.id class Meta: model = Customer fields = ('id', 'may_order', 'short_basket_name', 'long_basket_name', 'email', 'email2', 'language', 'phone1', 'phone2', 'address', 'city', 'bank_account1', 'bank_account2', 'date_balance', 'balance', 'price_list_multiplier', 'membership_fee_valid_until', 'last_membership_fee', 'last_membership_fee_date', 'participation', 'purchase', 'represent_this_buyinggroup', 'is_group', 'is_active', 'delivery_point', 'zero_waste', 'valid_email') export_order = fields import_id_fields = ('id', ) skip_unchanged = True report_skipped = False use_transactions = False
class PurchaseResource(resources.ModelResource): id = fields.Field(attribute="id", widget=IdWidget(), readonly=True) short_name = fields.Field(attribute="permanence__short_name", readonly=True) date = fields.Field( attribute="permanence__permanence_date", widget=DateWidgetExcel(), readonly=True ) product_id = fields.Field(attribute="offer_item__product__id", readonly=True) product = fields.Field(attribute="offer_item__get_long_name", readonly=True) producer_id = fields.Field(attribute="producer__id", readonly=True) producer_name = fields.Field( attribute="producer__short_profile_name", readonly=True ) customer_id = fields.Field(attribute="customer__id", readonly=True) customer_name = fields.Field(attribute="customer__short_basket_name", readonly=True) unit_deposit = fields.Field( attribute="offer_item__unit_deposit", widget=TwoMoneysWidget(), readonly=True ) producer_unit_price_wo_tax = fields.Field( attribute="offer_item__producer_unit_price_wo_tax", widget=TwoMoneysWidget(), readonly=True, ) producer_vat = fields.Field( attribute="offer_item__producer_vat", widget=TwoMoneysWidget(), readonly=True ) customer_unit_price = fields.Field( attribute="offer_item__customer_unit_price", widget=TwoMoneysWidget(), readonly=True, ) customer_vat = fields.Field( attribute="offer_item__customer_vat", widget=TwoMoneysWidget(), readonly=True ) quantity_invoiced = fields.Field( attribute="quantity_invoiced", widget=FourDecimalsWidget(), readonly=True ) producer_row_price = fields.Field( attribute="purchase_price", widget=TwoMoneysWidget(), readonly=True ) customer_row_price = fields.Field( attribute="selling_price", widget=TwoMoneysWidget(), readonly=True ) vat_level = fields.Field( attribute="offer_item__vat_level", widget=ChoiceWidget(LUT_ALL_VAT, LUT_ALL_VAT_REVERSE), readonly=True, ) class Meta: model = Purchase fields = ( "id", "date", "short_name", "producer_id", "producer_name", "product_id", "product", "customer_id", "customer_name", "quantity_invoiced", "unit_deposit", "producer_unit_price_wo_tax", "producer_vat", "customer_unit_price", "customer_vat", "producer_row_price", "customer_row_price", "vat_level", "comment", ) export_order = fields import_id_fields = ("id",) skip_unchanged = True report_skipped = False use_transactions = False
class ProducerResource(resources.ModelResource): id = fields.Field(attribute="id", widget=IdWidget(), readonly=True) phone1 = fields.Field(attribute="phone1", widget=CharWidget(), readonly=False) phone2 = fields.Field(attribute="phone2", widget=CharWidget(), readonly=False) price_list_multiplier = fields.Field( attribute="price_list_multiplier", default=DECIMAL_ONE, widget=TwoDecimalsWidget(), readonly=False, ) date_balance = fields.Field(attribute="get_admin_date_balance", widget=DateWidgetExcel(), readonly=True) balance = fields.Field(attribute="get_admin_balance", widget=TwoMoneysWidget(), readonly=True) invoice_by_basket = fields.Field( attribute="invoice_by_basket", default=False, widget=DecimalBooleanWidget(), readonly=False, ) sort_products_by_reference = fields.Field( attribute="sort_products_by_reference", default=False, widget=DecimalBooleanWidget(), readonly=False, ) represent_this_buyinggroup = fields.Field( attribute="represent_this_buyinggroup", default=False, widget=DecimalBooleanWidget(), readonly=True, ) is_active = fields.Field(attribute="is_active", widget=DecimalBooleanWidget(), readonly=True) reference_site = fields.Field(attribute="reference_site", readonly=True) def before_save_instance(self, instance, using_transactions, dry_run): """ Override to add additional logic. """ producer_qs = Producer.objects.filter( short_profile_name=instance.short_profile_name).order_by("?") if instance.id is not None: producer_qs = producer_qs.exclude(id=instance.id) if producer_qs.exists(): raise ValueError( _("The short_basket_name {} is already used by another producer." ).format(instance.short_profile_name)) class Meta: model = Producer fields = ( "id", "short_profile_name", "long_profile_name", "email", "email2", "email3", "language", "phone1", "phone2", "fax", "address", "invoice_by_basket", "sort_products_by_reference", "producer_price_are_wo_vat", "price_list_multiplier", "reference_site", "bank_account", "date_balance", "balance", "represent_this_buyinggroup", "is_active", ) export_order = fields import_id_fields = ("id", ) skip_unchanged = True report_skipped = False use_transactions = False
class ProducerResource(resources.ModelResource): id = fields.Field(attribute='id', widget=IdWidget(), readonly=True) phone1 = fields.Field(attribute='phone1', default='1234', widget=CharWidget(), readonly=False) phone2 = fields.Field(attribute='phone2', widget=CharWidget(), readonly=False) price_list_multiplier = fields.Field(attribute='price_list_multiplier', default=DECIMAL_ONE, widget=TwoDecimalsWidget(), readonly=False) date_balance = fields.Field(attribute='get_admin_date_balance', widget=DateWidgetExcel(), readonly=True) balance = fields.Field(attribute='get_admin_balance', widget=TwoMoneysWidget(), readonly=True) invoice_by_basket = fields.Field(attribute='invoice_by_basket', default=False, widget=DecimalBooleanWidget(), readonly=False) manage_replenishment = fields.Field(attribute='manage_replenishment', default=False, widget=DecimalBooleanWidget(), readonly=False) sort_products_by_reference = fields.Field( attribute='sort_products_by_reference', default=False, widget=DecimalBooleanWidget(), readonly=False) is_resale_price_fixed = fields.Field(attribute='is_resale_price_fixed', default=False, widget=DecimalBooleanWidget(), readonly=False) represent_this_buyinggroup = fields.Field( attribute='represent_this_buyinggroup', default=False, widget=DecimalBooleanWidget(), readonly=True) is_active = fields.Field(attribute='is_active', widget=DecimalBooleanWidget(), readonly=True) reference_site = fields.Field(attribute='reference_site', readonly=True) def before_save_instance(self, instance, using_transactions, dry_run): """ Override to add additional logic. """ if not settings.REPANIER_SETTINGS_STOCK: if instance.manage_replenishment: raise ValueError( _("Manage replenishment must be set to False because this option is not activated." )) producer_qs = Producer.objects.filter( short_profile_name=instance.short_profile_name).order_by('?') if instance.id is not None: producer_qs = producer_qs.exclude(id=instance.id) if producer_qs.exists(): raise ValueError( _("The short_basket_name {} is already used by another producer." ).format(instance.short_profile_name)) class Meta: model = Producer fields = ('id', 'short_profile_name', 'long_profile_name', 'email', 'email2', 'email3', 'language', 'phone1', 'phone2', 'fax', 'address', 'invoice_by_basket', 'manage_replenishment', 'sort_products_by_reference', 'producer_pre_opening', 'producer_price_are_wo_vat', 'price_list_multiplier', 'is_resale_price_fixed', 'reference_site', 'bank_account', 'date_balance', 'balance', 'represent_this_buyinggroup', 'is_active') export_order = fields import_id_fields = ('id', ) skip_unchanged = True report_skipped = False use_transactions = False