Exemplo n.º 1
0
 def test_export_supplier_pricelist(self):
     """Export supplier pricelist to file."""
     supplier_pricelist_filepath = "tmp/test_supplier_pricelist.csv"
     item_count = 5
     supplier_items = []
     for i in range(item_count):
         inventory_item = random_inventory_item()
         # pylint:disable=no-member
         self.session.add(inventory_item)
         supplier_item = random_supplier_item(inventory_item)
         # pylint:disable=no-member
         self.session.add(supplier_item)
         supplier_items.append(supplier_item)
     export_supplier_pricelist(supplier_pricelist_filepath, supplier_items)
     file = open(supplier_pricelist_filepath)
     supplier_pricelist_reader = csv.DictReader(file, SPL_FIELDNAMES)
     supplier_pricelist_items = list(supplier_pricelist_reader)
     for i, supplier_pricelist_item in enumerate(supplier_pricelist_items):
         supplier_item = supplier_items[i]
         expected_item_values = {
             "supplier_code": supplier_item.code,
             "supp_item_code": supplier_item.item_code,
             "item_code": supplier_item.inventory_item.code,
             "supp_price_1": str(supplier_item.buy_price),
         }
         for key, expected_value in expected_item_values.items():
             value = supplier_pricelist_item[key]
             self.assertEqual(
                 expected_value, value,
                 "'{}' != '{}' for key: {}".format(expected_value, value,
                                                   key))
     file.close()
     delete_temporary_file(supplier_pricelist_filepath)
Exemplo n.º 2
0
    def test_apply_price_rule(self):
        """Recalculate sell prices on an item."""
        price_rule = random_price_rule()
        price_rule.price_0_factor = Decimal("6.00")
        price_rule.price_1_factor = Decimal("5.00")
        price_rule.price_2_factor = Decimal("4.00")
        price_rule.price_3_factor = Decimal("3.00")
        price_rule.price_4_factor = Decimal("2.00")
        # pylint:disable=no-member
        self.session.add(price_rule)

        inventory_item = random_inventory_item()
        inventory_item.replacement_cost = Decimal("10.00")
        # pylint:disable=no-member
        self.session.add(inventory_item)

        price_region_item = PriceRegionItem(
            code="",
            inventory_item=inventory_item,
            price_rule=price_rule,
            tax_code=TaxCode.TAXABLE,
            quantity_1=99999999,
            quantity_2=99999999,
            quantity_3=99999999,
            quantity_4=99999999,
            price_0=Decimal("24.00"),
            price_1=Decimal("20.00"),
            price_2=Decimal("16.00"),
            price_3=Decimal("12.00"),
            price_4=Decimal("8.00"),
            rrp_excl_tax=Decimal("0.00"),
            rrp_incl_tax=Decimal("0.00")
        )
        self.session.add(price_region_item)

        price_changes = apply_price_rule(price_region_item)

        calculated_prices = [
            price_region_item.price_0,
            price_region_item.price_1,
            price_region_item.price_2,
            price_region_item.price_3,
            price_region_item.price_4,
        ]
        expected_prices = [
            Decimal("59.9545"),  # 65.95 incl tax
            Decimal("49.9545"),  # 54.95 incl tax
            Decimal("39.9545"),  # 43.95 incl tax
            Decimal("29.9545"),  # 32.95 incl tax
            Decimal("20.0000"),  # 22.00 incl tax
        ]

        self.assertListEqual(expected_prices, calculated_prices)
        self.assertIsNotNone(price_changes)
Exemplo n.º 3
0
 def test_export_web_data_updates_report(self):
     """Export web data updates to file."""
     filepath = "tmp/test_export_web_data_updates_report.xlsx"
     item_count = 5
     inventory_items = []
     for i in range(item_count):
         inventory_item = random_inventory_item()
         web_sortcode = random_web_sortcode()
         inventory_web_data_item = random_inventory_web_data_item(
             inventory_item, web_sortcode)
         inventory_items.append(inventory_item)
     export_web_data_updates_report(filepath, inventory_items)
Exemplo n.º 4
0
 def test_export_downloaded_images_report(self):
     """Export downloaded images report to file."""
     report_filepath = "tmp/test_export_downloaded_images_report.xlsx"
     images = []
     missing_images = []
     item_count = 5
     for i in range(item_count):
         inventory_item = random_inventory_item()
         filename = "{}.jpg".format(inventory_item.code) if i > 0 else None
         images.append({
             "inventory_item": inventory_item,
             "source": random_string(3),
             "filename": filename
         })
     for i in range(item_count):
         inventory_item = random_inventory_item()
         missing_images.append(inventory_item)
     export_downloaded_images_report(report_filepath, images,
                                     missing_images)
     # TODO validate values in rows.
     delete_temporary_file(report_filepath)
Exemplo n.º 5
0
 def test_export_supplier_price_changes_report(self):
     """Export supplier price updates to XLSX report."""
     report_filepath = "tmp/test_supplier_price_changes_report.xlsx"
     item_count = 5
     price_changes = []
     # pylint:disable=unused-variable
     for i in range(item_count):
         inventory_item = random_inventory_item()
         # pylint:disable=no-member
         self.session.add(inventory_item)
         supplier_item = random_supplier_item(inventory_item)
         # pylint:disable=no-member
         self.session.add(supplier_item)
         price_was = supplier_item.buy_price
         price_diff = price_was * Decimal(random.randint(-10, 10)) / 100
         price_diff_percentage = Decimal(1)
         if price_was != Decimal(0):
             price_diff_percentage = price_diff / price_was
         price_now = price_was + price_diff
         price_changes.append({
             "supplier_item":
             supplier_item,
             # Create price difference between +/- 10%.
             "price_was":
             price_was,
             "price_now":
             price_now,
             "price_diff":
             price_diff,
             "price_diff_percentage":
             price_diff_percentage,
         })
     uom_errors = []
     export_supplier_price_changes_report(report_filepath, price_changes,
                                          uom_errors)
     report_reader = ReportReader(report_filepath)
     fieldnames = [
         "item_code",
         "supplier",
         "brand",
         "apn",
         "description",
         "price_was",
         "price_now",
         "price_diff",
         "price_diff_%",
     ]
     for fieldname in fieldnames:
         self.assertIn(fieldname, report_reader.fieldnames)
     data = report_reader.load()
     self.assertEqual(item_count, len(data))
     delete_temporary_file(report_filepath)
Exemplo n.º 6
0
 def test_export_web_product_menu_data(self):
     """Export inventory web data to file."""
     filepath = "tmp/test_export_web_product_menu_data.csv"
     item_count = 5
     inventory_items = []
     for i in range(item_count):
         inventory_item = random_inventory_item()
         web_sortcode = random_web_sortcode()
         inventory_web_data_item = random_inventory_web_data_item(
             inventory_item, web_sortcode)
         inventory_items.append(inventory_item)
     export_web_product_menu_data(filepath, inventory_items)
     delete_temporary_file(filepath)
Exemplo n.º 7
0
    def test_fetch_url(self):
        """Fetch image URLs."""
        inventory_item = random_inventory_item()
        # pylint:disable=no-member
        for fetcher_class in FETCHER_CLASSES:
            supplier_item = random_supplier_item(inventory_item)
            supplier_item.code = fetcher_class.__name__
            # pylint:disable=no-member

        fetchers = get_fetchers(inventory_item)
        for fetcher in fetchers:
            url = fetcher.get_image_url(inventory_item)
            self.assertIsInstance(url, str)
Exemplo n.º 8
0
    def test_AVD_fetch_url(self):
        """Fetch image URL from Avery website."""
        inventory_item = random_inventory_item()
        supplier_item = random_supplier_item(inventory_item)
        supplier_item.code = "AVD"
        supplier_item.item_code = random_string(20)
        # pylint:disable=no-member
        self.session.commit()
        fetcher = get_fetchers(inventory_item)[0]
        url = fetcher.get_image_url(inventory_item)
        self.assertIsNone(url)

        inventory_item = random_inventory_item()
        supplier_item = random_supplier_item(inventory_item)
        supplier_item.code = "AVD"
        supplier_item.item_code = "44540"
        fetchers = get_fetchers(inventory_item)
        self.assertEqual(1, len(fetchers))
        self.session.commit()
        fetcher = get_fetchers(inventory_item)[0]
        expected_url = "https://www.averyproducts.com.au/sites/avery.au/files/styles/scale_1_1_ratio_style/public/avery_importer/media/44540_pac.jpg?itok=L9_umi6U"
        fetcher = get_fetchers(inventory_item)[0]
        url = fetcher.get_image_url(inventory_item)
        self.assertEqual(url, expected_url)
Exemplo n.º 9
0
 def test_download_image(self):
     """Download image"""
     sku = "107001"
     inventory_item = random_inventory_item()
     supplier_item = random_supplier_item(inventory_item)
     supplier_item.code = "ACO"
     supplier_item.item_code = sku
     expected_filepath = os.path.join("tmp",
                                      "{}.jpg".format(inventory_item.code))
     fetchers = get_fetchers(inventory_item)
     for fetcher in fetchers:
         filepath = fetcher.download_image(inventory_item, "tmp")
         self.assertTrue(os.path.exists(filepath))
         self.assertEqual(expected_filepath, filepath)
         os.remove(filepath)
Exemplo n.º 10
0
 def test_export_tickets_list(self):
     """Export tickets list to file."""
     tickets_list_filepath = "tmp/test_tickets_list.txt"
     item_count = 5
     warehouse_stock_items = []
     for i in range(item_count):
         inventory_item = random_inventory_item()
         warehouse_stock_item = random_warehouse_stock_item(inventory_item)
         warehouse_stock_items.append(warehouse_stock_item)
     export_tickets_list(tickets_list_filepath, warehouse_stock_items)
     file = open(tickets_list_filepath)
     item_codes = [row[0] for row in list(csv.reader(file))]
     file.close()
     for i, item_code in enumerate(item_codes):
         expected_item_code = warehouse_stock_items[i].inventory_item.code
         self.assertEqual(item_code, expected_item_code)
     delete_temporary_file(tickets_list_filepath)
Exemplo n.º 11
0
    def test_update_product_menu(self):
        """Map price rules to web sortcodes."""
        item_count = 10
        inventory_items = []
        web_sortcode_mappings = {}

        # pylint:disable=unused-variable
        for i in range(item_count):
            inventory_item = random_inventory_item()
            # pylint:disable=no-member
            self.session.add(inventory_item)
            web_sortcode = random_web_sortcode()
            # pylint:disable=no-member
            self.session.add(web_sortcode)
            price_rule = random_price_rule()
            # pylint:disable=no-member
            self.session.add(price_rule)
            price_region_item = random_price_region_item(
                inventory_item, price_rule)
            # pylint:disable=no-member
            self.session.add(price_region_item)
            web_sortcode_mappings[price_rule.code] = web_sortcode
            inventory_web_data_item = random_inventory_web_data_item(
                inventory_item, web_sortcode)
            # pylint:disable=no-member
            self.session.add(inventory_web_data_item)
            # pylint:disable=no-member
            self.session.commit()
            inventory_items.append(inventory_item)

        updated_inventory_items = update_product_menu(inventory_items,
                                                      web_sortcode_mappings,
                                                      self.session)
        self.assertEqual(10, len(updated_inventory_items))

        web_sortcodes = web_sortcode_mappings.values()
        for inventory_item in updated_inventory_items:
            self.assertIn(inventory_item.inventory_web_data_item.web_sortcode,
                          web_sortcodes)
Exemplo n.º 12
0
    def test_update_supplier_items(self):
        """Apply supplier pricelist prices to supplier items."""
        item_count = 10
        supplier_items = []
        # pylint:disable=unused-variable
        for i in range(item_count):
            inventory_item = random_inventory_item()
            # pylint:disable=no-member
            self.session.add(inventory_item)
            supplier_item = random_supplier_item(inventory_item)
            # pylint:disable=no-member
            self.session.add(supplier_item)
            supplier_items.append(supplier_item)

        def random_supplier_pricelist_item(supplier_item):
            supplier_code = supplier_item.code
            item_code = supplier_item.item_code
            # Mock a price increase by multiplying the existing buy price.
            buy_price = supplier_item.buy_price * \
                Decimal(random.randint(200, 400)) / 100
            return {
                "item_code": supplier_item.inventory_item.code,
                "supplier_code": supplier_code,
                "supp_item_code": item_code,
                "supp_price_1": buy_price,
                "supp_uom": supplier_item.uom,
                "supp_conv_factor": supplier_item.conv_factor,
            }

        supplier_pricelist_items = [
            random_supplier_pricelist_item(item) for item in supplier_items
        ]

        price_changes, uom_errors = update_supplier_items(
            supplier_pricelist_items, self.session)
        self.assertEqual(10, len(price_changes))