def test_inventory(self):
        branch = get_current_branch(self.store)
        product = self.create_product(branch=branch, stock=10)
        editor = ProductStockQuantityEditor(self.store, product, branch)
        self.check_editor(editor, 'editor-product-stock-quantity-inventory-show')

        # Update editor
        editor.quantity.update(20)
        editor.reason.update('test case')

        inventories_before = self.store.find(Inventory).count()

        # Confirm
        self.click(editor.main_dialog.ok_button)

        # Check data
        inventories_after = self.store.find(Inventory).count()
        self.assertEqual(inventories_after, inventories_before + 1)

        stock_item = product.storable.get_stock_item(branch, batch=None)
        self.assertEqual(stock_item.quantity, 20)

        # Check Inventory
        inventory = self.store.find(Inventory).order_by(Inventory.te_id).last()
        # The inventory should have only one item
        item = inventory.get_items().one()

        self.assertEqual(item.recorded_quantity, 10)
        self.assertEqual(item.counted_quantity, 20)
        self.assertEqual(item.actual_quantity, 20)
        self.assertEqual(item.is_adjusted, True)
        self.assertEqual(inventory.status, Inventory.STATUS_CLOSED)
    def test_other_branch(self):
        other_branch = self.create_branch()
        product = self.create_product(branch=other_branch, stock=10)

        with self.sysparam(SYNCHRONIZED_MODE=True):
            editor = ProductStockQuantityEditor(self.store, product, other_branch)
            editor.quantity.set_text('9')
            # Do not let the user decrease stock from other branches in
            # synchronized mode
            self.assertInvalid(editor, ['quantity'])

            # The user can increase stock from other branches
            editor.quantity.set_text('11')
            editor.reason.update('test')
            self.assertValid(editor, ['quantity'])
            self.click(editor.main_dialog.ok_button)
    def test_same_branch(self):
        branch = get_current_branch(self.store)
        product = self.create_product(branch=branch, stock=10)

        editor = ProductStockQuantityEditor(self.store, product, branch)
        # Using set_text because update() will not show the validation
        editor.quantity.set_text('-4')
        editor.reason.update('test')
        # Do not let the user decrease stock to a negative number
        self.assertInvalid(editor, ['quantity'])

        # Let the user decrease stock
        editor.quantity.update(9)
        self.assertValid(editor, ['quantity'])

        # Let the user increase stock
        editor.quantity.update(11)
        self.assertValid(editor, ['quantity'])
        self.click(editor.main_dialog.ok_button)
    def test_initial_stock(self):
        branch = get_current_branch(self.store)
        product = self.create_product(branch=branch, storable=False)
        product.manage_stock = False
        editor = ProductStockQuantityEditor(self.store, product, branch)
        self.check_editor(editor, 'editor-product-stock-quantity-initial-show')

        # Update editor
        editor.quantity.update(15)
        editor.cost.update('3.45')

        stock_items_before = self.store.find(ProductStockItem).count()

        # Confirm
        self.click(editor.main_dialog.ok_button)

        # Check data
        stock_items_after = self.store.find(ProductStockItem).count()
        self.assertEqual(stock_items_after, stock_items_before + 1)

        stock_item = product.storable.get_stock_item(branch, batch=None)
        self.assertEqual(stock_item.quantity, 15)
        self.assertEqual(stock_item.stock_cost, Decimal('3.45'))