Example #1
0
    def create_filters(self):
        items = [(_('Active'), True), (_('Inactive'), False)]
        items.insert(0, (_('Any'), None))

        status_filter = ComboSearchFilter(_('Show transporters with status'),
                                          items)
        status_filter.select(None)
        self.add_filter(status_filter, SearchFilterPosition.TOP, ['is_active'])
Example #2
0
 def create_filters(self):
     # status filter
     statuses = [(desc, i) for i, desc in Loan.statuses.items()]
     statuses.insert(0, (_(u'Any'), None))
     status_filter = ComboSearchFilter(_(u'with status:'), statuses)
     status_filter.select(None)
     self.add_filter(status_filter,
                     columns=['loan_status'],
                     position=SearchFilterPosition.TOP)
Example #3
0
 def create_filters(self):
     statuses = [(value, key) for key, value in Branch.statuses.items()]
     statuses.insert(0, (_('Any'), None))
     status_filter = ComboSearchFilter(_('Show branches with status'),
                                       statuses)
     status_filter.select(None)
     executer = self.search.get_query_executer()
     executer.add_filter_query_callback(status_filter,
                                        self._get_status_query)
     self.search.add_filter(status_filter, SearchFilterPosition.TOP)
Example #4
0
    def create_sellable_filter(self, label=None):
        from stoqlib.domain.sellable import Sellable
        items = [(desc, status) for status, desc in Sellable.statuses.items()]
        items.insert(0, (_(u"Any"), None))

        if label is None:
            label = _('With status:')
        sellable_filter = ComboSearchFilter(label, items)
        # Select status available by default
        sellable_filter.select(Sellable.STATUS_AVAILABLE)

        return sellable_filter
Example #5
0
    def create_filters(self):
        self.set_text_field_columns(['description', 'identifier_str'])
        executer = self.search.get_query_executer()
        executer.add_query_callback(self._get_query)

        # Status
        items = [(v, k) for k, v in Till.statuses.items()
                 if k != Till.STATUS_PENDING]
        items.insert(0, (_(u'Any'), None))
        status_filter = ComboSearchFilter(_(u'Show entries of type'), items)
        status_filter.select(Till.STATUS_OPEN)
        self.add_filter(status_filter,
                        position=SearchFilterPosition.TOP,
                        columns=['status'])
Example #6
0
    def create_payment_filter(self, label=None):
        from stoqlib.domain.payment.method import PaymentMethod
        methods = PaymentMethod.get_active_methods(self.store)
        items = [(_('Any'), None)]
        for method in methods:
            if method.method_name == 'multiple':
                continue
            items.append((method.description, method))

        if not label:
            label = _('Method:')
        payment_filter = ComboSearchFilter(label, items)
        payment_filter.select(None)

        return payment_filter
Example #7
0
    def create_filters(self):
        statuses = [(v, k) for k, v in Client.statuses.items()]
        statuses.insert(0, (_('Any'), None))
        status_filter = ComboSearchFilter(_('Show clients with status'),
                                          statuses)
        status_filter.select(None)
        self.add_filter(status_filter, SearchFilterPosition.TOP, ['status'])

        if self._birth_date:
            birthday_filter = self.search.add_filter_by_attribute(
                'birth_date',
                _('Birthday'),
                datetime.date,
                callback=self.birthday_search)
            # FIXME: The fifth position is a search by day. This is done
            # elsewhere too but we should not hardcode it. Try to
            # find a better solution in the future and fix everything
            birthday_filter.mode.select_item_by_position(5)
            birthday_filter.start_date.set_date(self._birth_date)
            self.search.refresh()
Example #8
0
class ProductionItemsSearch(ProductSearch):
    title = _(u'Production Items')
    search_spec = ProductionItemView
    report_class = ProductionItemReport
    csv_data = None
    has_print_price_button = False
    text_field_columns = [ProductionItemView.description,
                          ProductionItemView.order_identifier_str]

    def __init__(self, store, hide_footer=True, hide_toolbar=True):
        ProductSearch.__init__(self, store, hide_footer=hide_footer,
                               hide_toolbar=hide_toolbar)

    #
    # SearchDialog
    #

    def create_filters(self):
        statuses = [(desc, i) for i, desc in ProductionOrder.statuses.items()]
        statuses.insert(0, (_(u'Any'), None))
        self.status_filter = ComboSearchFilter(_('order status:'), statuses)
        self.status_filter.select(ProductionOrder.ORDER_PRODUCING)
        self.add_filter(self.status_filter, columns=['order_status'],
                        position=SearchFilterPosition.TOP)

    def get_columns(self):
        return [IdentifierColumn('order_identifier', title=_(u"Order #"),
                                 sorted=True),
                SearchColumn('category_description', title=_(u'Category'),
                             data_type=str),
                SearchColumn('description', title=_(u'Description'),
                             data_type=str, expand=True),
                SearchColumn('unit_description', title=_(u'Unit'),
                             data_type=str),
                SearchColumn('quantity', title=_(u'To Produce'),
                             data_type=Decimal),
                SearchColumn('produced', title=_(u'Produced'),
                             data_type=Decimal),
                SearchColumn('lost', title=_(u'Lost'), data_type=Decimal,
                             visible=False)]
Example #9
0
    def create_branch_filter(self, label=None, column=None):
        """Returns a new branch filter.

        :param label: The label to be used for the filter
        :param column: When provided, besides creating the filter, we will also
          add it to the interface, filtering by the informed column.
        """
        items = api.get_branches_for_filter(self.store, use_id=True)
        if not label:
            label = _('Branch:')

        if column and not isinstance(column, list):
            column = [column]

        branch_filter = ComboSearchFilter(label, items)
        current = api.get_current_branch(self.store)
        branch_filter.select(current.id)
        if column:
            self.add_filter(branch_filter, columns=column,
                            position=SearchFilterPosition.TOP)

        return branch_filter
Example #10
0
class TransferOrderSearch(SearchDialog):
    title = _(u"Transfer Order Search")
    size = (750, 500)
    search_spec = TransferOrderView
    report_class = TransferOrderReport
    selection_mode = Gtk.SelectionMode.MULTIPLE

    def __init__(self, store):
        SearchDialog.__init__(self, store)
        self._setup_widgets()

    def _show_transfer_order_details(self, order_view):
        transfer_order = order_view.transfer_order
        with api.new_store() as store:
            model = store.fetch(transfer_order)
            run_dialog(TransferOrderDetailsDialog, self, store, model)
            store.retval = store.get_pending_count() > 0

    def _setup_widgets(self):
        self.results.connect('row_activated', self.on_row_activated)
        self.update_widgets()

    def _get_status_values(self):
        items = [(str(value), key)
                 for key, value in TransferOrder.statuses.items()]
        items.insert(0, (_('Any'), None))
        return items

    #
    # SearchDialog Hooks
    #

    def update_widgets(self):
        orders = self.results.get_selected_rows()
        has_one_selected = len(orders) == 1
        self.set_details_button_sensitive(has_one_selected)
        self.set_print_button_sensitive(has_one_selected)

    def create_filters(self):
        self.set_text_field_columns([
            'source_branch_name', 'destination_branch_name', 'identifier_str'
        ])

        # Date
        self.date_filter = DateSearchFilter(_('Date:'))
        self.add_filter(self.date_filter, columns=['open_date', 'finish_date'])

        # Status
        self.status_filter = ComboSearchFilter(_('With status:'),
                                               self._get_status_options())
        self.status_filter.select('pending')
        executer = self.search.get_query_executer()
        executer.add_filter_query_callback(self.status_filter,
                                           self._get_status_query)
        self.add_filter(self.status_filter, position=SearchFilterPosition.TOP)

    def _get_status_options(self):
        return [
            (_('All transfers'), None),
            (_('Pending receive'), 'pending'),
            (_('Received'), 'received'),
            (_('Sent'), 'sent'),
            (_('Cancelled'), 'cancelled'),
        ]

    def _get_status_query(self, state):
        current_branch = api.get_current_branch(self.store)
        if state.value == 'pending':
            return And(
                TransferOrder.status == TransferOrder.STATUS_SENT,
                TransferOrder.destination_branch_id == current_branch.id)
        elif state.value == 'received':
            return And(
                TransferOrder.status == TransferOrder.STATUS_RECEIVED,
                TransferOrder.destination_branch_id == current_branch.id)
        elif state.value == 'sent':
            return And(
                TransferOrder.source_branch_id == current_branch.id,
                Not(TransferOrder.status == TransferOrder.STATUS_CANCELLED))
        elif state.value == 'cancelled':
            return And(TransferOrder.status == TransferOrder.STATUS_CANCELLED,
                       TransferOrder.source_branch_id == current_branch.id)
        else:
            return Or(TransferOrder.source_branch_id == current_branch.id,
                      TransferOrder.destination_branch_id == current_branch.id)

    def get_columns(self):
        return [
            IdentifierColumn('identifier', title=_('Transfer #')),
            SearchColumn('transfer_order.status_str',
                         _('Status'),
                         data_type=str,
                         valid_values=self._get_status_values(),
                         search_attribute='status',
                         width=100),
            SearchColumn('open_date',
                         _('Open date'),
                         data_type=datetime.date,
                         sorted=True,
                         width=100),
            SearchColumn('finish_date',
                         _('Finish Date'),
                         data_type=datetime.date,
                         width=100,
                         visible=False),
            SearchColumn('source_branch_name',
                         _('Source'),
                         data_type=str,
                         expand=True),
            SearchColumn('destination_branch_name',
                         _('Destination'),
                         data_type=str,
                         width=220),
            Column('total_items',
                   _('Items'),
                   data_type=Decimal,
                   format_func=format_quantity,
                   width=110)
        ]

    #
    # Callbacks
    #

    def on_row_activated(self, klist, view):
        self._show_transfer_order_details(view)

    def on_details_button_clicked(self, button):
        self._show_transfer_order_details(self.results.get_selected_rows()[0])
Example #11
0
class StockApp(ShellApp):
    app_title = _('Stock')
    gladefile = "stock"
    search_spec = ProductFullStockView
    search_labels = _('Matching:')
    report_table = SimpleProductReport
    pixbuf_converter = converter.get_converter(GdkPixbuf.Pixbuf)

    #
    # Application
    #

    def create_actions(self):
        group = get_accels('app.stock')
        actions = [
            ("NewReceiving", None, _("Order _receival..."),
             group.get('new_receiving')),
            ('NewTransfer', Gtk.STOCK_CONVERT, _('Transfer...'),
             group.get('transfer_product')),
            ('NewStockDecrease', None, _('Stock decrease...'),
             group.get('stock_decrease')),
            ('StockInitial', Gtk.STOCK_GO_UP, _('Register initial stock...')),
            ("LoanNew", None, _("Loan...")),
            ("LoanClose", None, _("Close loan...")),
            ("SearchPurchaseReceiving", None, _("Received purchases..."),
             group.get('search_receiving'),
             _("Search for received purchase orders")),
            ("SearchProductHistory", None, _("Product history..."),
             group.get('search_product_history'),
             _("Search for product history")),
            ("SearchStockDecrease", None, _("Stock decreases..."), '',
             _("Search for manual stock decreases")),
            ("SearchPurchasedStockItems", None, _("Purchased items..."),
             group.get('search_purchased_stock_items'),
             _("Search for purchased items")),
            ("SearchBrandItems", None, _("Brand items..."),
             group.get('search_brand_items'),
             _("Search for brand items on stock")),
            ("SearchBrandItemsByBranch", None, _("Brand item by branch..."),
             group.get('search_brand_by_branch'),
             _("Search for brand items by branch on stock")),
            ("SearchBatchItems", None, _("Batch items..."),
             group.get('search_batch_items'),
             _("Search for batch items on stock")),
            ("SearchStockItems", None, _("Stock items..."),
             group.get('search_stock_items'), _("Search for items on stock")),
            ("SearchTransfer", None, _("Transfers..."),
             group.get('search_transfers'), _("Search for stock transfers")),
            ("SearchClosedStockItems", None, _("Closed stock Items..."),
             group.get('search_closed_stock_items'),
             _("Search for closed stock items")),
            ("LoanSearch", None, _("Loans...")),
            ("LoanSearchItems", None, _("Loan items...")),
            ("SearchTransferItems", None, _("Transfer items...")),
            ("SearchReturnedItems", None, _("Returned items...")),
            ("SearchPendingReturnedSales", None,
             _("Pending returned sales...")),
            ("ProductMenu", None, _("Product")),
            ("PrintLabels", None, _("Print labels...")),
            ("ManageStock", None, _("Manage stock...")),
            ("ProductStockHistory", Gtk.STOCK_INFO, _("History..."),
             group.get('history'),
             _('Show the stock history of the selected product')),
            ("EditProduct", Gtk.STOCK_EDIT, _("Edit..."),
             group.get('edit_product'),
             _("Edit the selected product, allowing you to change it's "
               "details")),
        ]
        self.stock_ui = self.add_ui_actions(actions)

        toggle_actions = [
            ('StockPictureViewer', None, _('Picture viewer'),
             group.get('toggle_picture_viewer')),
        ]
        self.add_ui_actions(toggle_actions, 'ToggleActions')
        self.set_help_section(_("Stock help"), 'app-stock')

    def create_ui(self):
        if api.sysparam.get_bool('SMART_LIST_LOADING'):
            self.search.enable_lazy_search()

        self.window.add_print_items([self.PrintLabels])
        self.window.add_export_items()
        self.window.add_extra_items(
            [self.StockInitial, self.LoanClose, self.StockPictureViewer])
        self.window.add_new_items([
            self.NewReceiving, self.NewTransfer, self.NewStockDecrease,
            self.LoanNew
        ])
        self.window.add_search_items([
            self.SearchPurchaseReceiving,
            self.SearchProductHistory,
            self.SearchTransfer,
            self.SearchTransferItems,
            self.SearchStockDecrease,
            self.SearchReturnedItems,
            self.SearchPurchasedStockItems,
            self.SearchStockItems,
            self.SearchBrandItems,
            self.SearchBrandItemsByBranch,
            self.SearchBatchItems,
            self.SearchClosedStockItems,
            self.SearchPendingReturnedSales,
            # Separator
            self.LoanSearch,
            self.LoanSearchItems,
        ])
        self._inventory_widgets = [
            self.NewTransfer, self.NewReceiving, self.StockInitial,
            self.NewStockDecrease, self.LoanNew, self.LoanClose
        ]
        self.register_sensitive_group(self._inventory_widgets,
                                      lambda: not self.has_open_inventory())

        self.image_viewer = None

        self.image = Gtk.Image()
        # FIXME: How are we goint to display an image preview?
        #self.edit_button = self.uimanager.get_widget('/toolbar/AppToolbarPH/EditProduct')
        #self.edit_button.set_icon_widget(self.image)
        self.image.show()

        self.search.set_summary_label(column='stock',
                                      label=_('<b>Stock Total:</b>'),
                                      format='<b>%s</b>',
                                      parent=self.get_statusbar_message_area())

    def get_domain_options(self):
        options = [
            ('fa-info-circle-symbolic', _('History'),
             'stock.ProductStockHistory', True),
            ('fa-edit-symbolic', _('Edit'), 'stock.EditProduct', True),
            ('', _('Print labels'), 'stock.PrintLabels', False),
            ('', _('Manage stock'), 'stock.ManageStock', False),
        ]

        return options

    def activate(self, refresh=True):
        if refresh:
            self.refresh()

        open_inventory = self.check_open_inventory()

        if not open_inventory:
            self.transfers_bar = self._create_pending_info_message()
            self.returned_bar = self._create_pending_returned_sale_message()
        else:
            self.transfers_bar = None
            self.returned_bar = None

        self._update_widgets()

        self.search.focus_search_entry()

    def deactivate(self):
        if self.transfers_bar:
            self.transfers_bar.hide()
        if self.returned_bar:
            self.returned_bar.hide()

        self._close_image_viewer()

    def set_open_inventory(self):
        self.set_sensitive(self._inventory_widgets, False)

    def create_filters(self):
        self.search.set_query(self._query)
        self.set_text_field_columns([
            'description', 'code', 'barcode', 'category_description',
            'manufacturer'
        ])
        branches = Branch.get_active_branches(self.store)
        self.branch_filter = ComboSearchFilter(
            _('Show by:'), api.for_combo(branches, empty=_("All branches")))
        self.branch_filter.select(api.get_current_branch(self.store))
        self.add_filter(self.branch_filter, position=SearchFilterPosition.TOP)

    def get_columns(self):
        return [
            SearchColumn('code',
                         title=_('Code'),
                         sorted=True,
                         sort_func=sort_sellable_code,
                         data_type=str,
                         width=130),
            SearchColumn('barcode',
                         title=_("Barcode"),
                         data_type=str,
                         width=130),
            SearchColumn('category_description',
                         title=_("Category"),
                         data_type=str,
                         width=100,
                         visible=False),
            SearchColumn('description',
                         title=_("Description"),
                         data_type=str,
                         expand=True,
                         ellipsize=Pango.EllipsizeMode.END),
            SearchColumn('manufacturer',
                         title=_("Manufacturer"),
                         data_type=str,
                         visible=False),
            SearchColumn('brand',
                         title=_("Brand"),
                         data_type=str,
                         visible=False),
            SearchColumn('model',
                         title=_("Model"),
                         data_type=str,
                         visible=False),
            SearchColumn('location',
                         title=_("Location"),
                         data_type=str,
                         width=100,
                         visible=False),
            QuantityColumn('stock',
                           title=_('Quantity'),
                           width=100,
                           use_having=True),
            SearchColumn('has_image',
                         title=_('Picture'),
                         data_type=bool,
                         width=80),
        ]

    #
    # Private API
    #

    def _open_image_viewer(self):
        assert self.image_viewer is None

        self.image_viewer = SellableImageViewer(size=(325, 325))
        self.image_viewer.toplevel.connect('delete-event',
                                           self.on_image_viewer_closed)
        self.image_viewer.show_all()

        self._update_widgets()

    def _close_image_viewer(self):
        if self.image_viewer is None:
            return

        self.image_viewer.destroy()
        self.image_viewer = None

    def _query(self, store):
        branch = self.branch_filter.get_state().value
        return self.search_spec.find_by_branch(store, branch)

    def _update_widgets(self):
        branch = api.get_current_branch(self.store)

        is_main_branch = self.branch_filter.get_state().value is branch
        item = self.results.get_selected()

        sellable = item and item.product.sellable
        if sellable:
            if item.has_image:
                # XXX:Workaround for a bug caused by the image domain refactoring
                # which left some existent thumbnail as None
                thumbnail = sellable.image.thumbnail
                if thumbnail is None:
                    # Create new store to create the thumbnail
                    with api.new_store() as new_store:
                        image = sellable.image
                        image = new_store.fetch(image)
                        size = (Image.THUMBNAIL_SIZE_WIDTH,
                                Image.THUMBNAIL_SIZE_HEIGHT)
                        image.thumbnail = get_thumbnail(image.image, size)
                        thumbnail = image.thumbnail
                pixbuf = self.pixbuf_converter.from_string(thumbnail)
            else:
                pixbuf = None

            self._update_edit_image(pixbuf)
            if self.image_viewer:
                self.image_viewer.set_sellable(sellable)
        else:
            self._update_edit_image()

        # Always let the user choose the manage stock option and do a proper
        # check there (showing a warning if he can't)
        self.set_sensitive([self.ManageStock], bool(item))
        self.set_sensitive([self.EditProduct, self.PrintLabels], bool(item))
        self.set_sensitive([self.ProductStockHistory],
                           bool(item) and is_main_branch)
        # We need more than one branch to be able to do transfers
        # Note that 'all branches' is not a real branch
        has_branches = len(self.branch_filter.combo) > 2

        transfer_active = self.NewTransfer.get_enabled()
        self.set_sensitive([self.NewTransfer], transfer_active
                           and has_branches)
        # Building a list of searches that we must disable if there is no
        # branches other than the main company
        searches = [
            self.SearchTransfer, self.SearchTransferItems,
            self.SearchPendingReturnedSales
        ]
        self.set_sensitive(searches, has_branches)

    def _update_edit_image(self, pixbuf=None):
        if not pixbuf:
            self.image.set_from_stock(Gtk.STOCK_EDIT,
                                      Gtk.IconSize.LARGE_TOOLBAR)
            return

        # FIXME: get this icon size from settings
        icon_size = 24
        pixbuf = pixbuf.scale_simple(icon_size, icon_size,
                                     GdkPixbuf.InterpType.BILINEAR)
        self.image.set_from_pixbuf(pixbuf)

    def _update_filter_slave(self, slave):
        self.refresh()

    def _transfer_stock(self):
        if self.check_open_inventory():
            return
        store = api.new_store()
        model = self.run_dialog(StockTransferWizard, store)
        store.confirm(model)
        store.close()
        self.refresh()

    def _receive_purchase(self):
        if self.check_open_inventory():
            return
        store = api.new_store()
        model = self.run_dialog(ReceivingOrderWizard, store)
        store.confirm(model)
        store.close()
        self.refresh()

    def _create_pending_info_message(self):
        branch = api.get_current_branch(self.store)
        n_transfers = TransferOrder.get_pending_transfers(self.store,
                                                          branch).count()

        if not n_transfers:
            return None

        msg = stoqlib_ngettext(_(u"You have %s incoming transfer"),
                               _(u"You have %s incoming transfers"),
                               n_transfers) % n_transfers
        info_bar = self.window.add_info_bar(Gtk.MessageType.QUESTION, msg)
        button = info_bar.add_button(_(u"Receive"), Gtk.ResponseType.OK)
        button.connect('clicked', self._on_info_transfers__clicked)

        return info_bar

    def _create_pending_returned_sale_message(self):
        branch = api.get_current_branch(self.store)
        n_returned = ReturnedSale.get_pending_returned_sales(
            self.store, branch).count()

        if not n_returned:
            return None

        msg = stoqlib_ngettext(_(u"You have %s returned sale to receive"),
                               _(u"You have %s returned sales to receive"),
                               n_returned) % n_returned
        info_returned_bar = self.window.add_info_bar(Gtk.MessageType.QUESTION,
                                                     msg)
        button = info_returned_bar.add_button(_(u"Returned sale"),
                                              Gtk.ResponseType.OK)
        button.connect('clicked', self._on_info_returned_sales__clicked)

        return info_returned_bar

    def _search_transfers(self):
        branch = api.get_current_branch(self.store)
        self.run_dialog(TransferOrderSearch, self.store)

        # After the search is closed we may want to update , or even hide the
        # message, if there is no pending transfer to receive
        if self.transfers_bar:
            n_transfers = TransferOrder.get_pending_transfers(
                self.store, branch).count()

            if n_transfers > 0:
                msg = stoqlib_ngettext(_(u"You have %s incoming transfer"),
                                       _(u"You have %s incoming transfers"),
                                       n_transfers) % n_transfers
                self.transfers_bar.set_message(msg)
            else:
                self.transfers_bar.hide()
        self.refresh()

    def _search_pending_returned_sales(self):
        with api.new_store() as store:
            self.run_dialog(PendingReturnedSaleSearch, store)

        branch = api.get_current_branch(self.store)
        # After the search is closed we may want to update , or even hide the
        # message, if there is no pending returned sale to receive
        if self.returned_bar:
            n_returned = ReturnedSale.get_pending_returned_sales(
                self.store, branch).count()

            if n_returned > 0:
                msg = stoqlib_ngettext(
                    _(u"You have %s returned sale to receive"),
                    _(u"You have %s returned sales to receive"),
                    n_returned) % n_returned
                self.returned_bar.set_message(msg)
            else:
                self.returned_bar.hide()
        self.refresh()

    #
    # Callbacks
    #

    def on_image_viewer_closed(self, window, event):
        self.image_viewer = None
        self.StockPictureViewer.set_state(GLib.Variant.new_boolean(False))

    def on_results__has_rows(self, results, product):
        self._update_widgets()

    def on_results__selection_changed(self, results, product):
        self._update_widgets()

    def on_ProductStockHistory__activate(self, button):
        selected = self.results.get_selected()
        sellable = selected.sellable
        self.run_dialog(ProductStockHistoryDialog,
                        self.store,
                        sellable,
                        branch=self.branch_filter.combo.get_selected())

    def on_ManageStock__activate(self, action):
        user = api.get_current_user(self.store)
        if not user.profile.check_app_permission(u'inventory'):
            return warning(
                _('Only users with access to the inventory app can'
                  ' change the stock quantity'))

        product = self.results.get_selected().product
        if product.is_grid:
            return warning(_("Can't change stock quantity of a grid parent"))

        if product.storable and product.storable.is_batch:
            return warning(
                _("It's not possible to change the stock quantity of"
                  " a batch product"))

        branch = self.branch_filter.combo.get_selected()
        if not branch:
            return warning(_('You must select a branch first'))

        with api.new_store() as store:
            self.run_dialog(ProductStockQuantityEditor,
                            store,
                            store.fetch(product),
                            branch=branch)

        if store.committed:
            self.refresh()

    def on_PrintLabels__activate(self, button):
        selected = self.results.get_selected()
        sellable = selected.sellable
        label_data = self.run_dialog(PrintLabelEditor, None, self.store,
                                     sellable)
        if label_data:
            print_labels(label_data, self.store)

    def on_EditProduct__activate(self, button):
        selected = self.results.get_selected()
        assert selected

        store = api.new_store()
        product = store.fetch(selected.product)

        model = self.run_dialog(ProductStockEditor, store, product)
        store.confirm(model)
        store.close()
        if model:
            self.refresh()

    def _on_info_transfers__clicked(self, button):
        self._search_transfers()

    def _on_info_returned_sales__clicked(self, button):
        self._search_pending_returned_sales()

    # Stock

    def on_NewReceiving__activate(self, button):
        self._receive_purchase()

    def on_NewTransfer__activate(self, button):
        self._transfer_stock()

    def on_NewStockDecrease__activate(self, action):
        if self.check_open_inventory():
            return
        store = api.new_store()
        model = self.run_dialog(StockDecreaseWizard, store)
        store.confirm(model)
        store.close()
        self.refresh()

    def on_StockInitial__activate(self, action):
        if self.check_open_inventory():
            return

        with api.new_store() as store:
            self.run_dialog(InitialStockDialog, store)

        if store.committed:
            self.refresh()

    def on_StockPictureViewer__change_state(self, action, value):
        action.set_state(value)
        if value.get_boolean():
            self._open_image_viewer()
        else:
            self._close_image_viewer()

    # Loan

    def on_LoanNew__activate(self, action):
        if self.check_open_inventory():
            return
        store = api.new_store()
        model = self.run_dialog(NewLoanWizard, store)
        store.confirm(model)
        store.close()
        self.refresh()

    def on_LoanClose__activate(self, action):
        if self.check_open_inventory():
            return
        store = api.new_store()
        model = self.run_dialog(CloseLoanWizard, store)
        store.confirm(model)
        store.close()
        self.refresh()

    def on_LoanSearch__activate(self, action):
        self.run_dialog(LoanSearch, self.store)

    def on_LoanSearchItems__activate(self, action):
        self.run_dialog(LoanItemSearch, self.store)

    # Search

    def on_SearchPurchaseReceiving__activate(self, button):
        self.run_dialog(PurchaseReceivingSearch, self.store)

    def on_SearchTransfer__activate(self, action):
        self._search_transfers()

    def on_SearchTransferItems__activate(self, action):
        self.run_dialog(TransferItemSearch, self.store)

    def on_SearchPendingReturnedSales__activate(self, action):
        self._search_pending_returned_sales()

    def on_SearchReturnedItems__activate(self, action):
        self.run_dialog(ReturnedItemSearch, self.store)

    def on_SearchPurchasedStockItems__activate(self, action):
        self.run_dialog(PurchasedItemsSearch, self.store)

    def on_SearchStockItems__activate(self, action):
        self.run_dialog(ProductStockSearch, self.store)

    def on_SearchBrandItems__activate(self, action):
        self.run_dialog(ProductBrandSearch, self.store)

    def on_SearchBrandItemsByBranch__activate(self, action):
        self.run_dialog(ProductBrandByBranchSearch, self.store)

    def on_SearchBatchItems__activate(self, action):
        self.run_dialog(ProductBatchSearch, self.store)

    def on_SearchClosedStockItems__activate(self, action):
        self.run_dialog(ProductClosedStockSearch, self.store)

    def on_SearchProductHistory__activate(self, action):
        self.run_dialog(ProductSearchQuantity, self.store)

    def on_SearchStockDecrease__activate(self, action):
        self.run_dialog(StockDecreaseSearch, self.store)
Example #12
0
class DeliverySearch(SearchEditor):
    """Delivery search implementation"""

    title = _('Delivery Search')
    search_spec = DeliveryView
    editor_class = DeliveryEditor
    has_new_button = False
    size = (750, 450)

    #
    #  Private
    #

    def _get_status_values(self):
        items = [(value, key) for key, value in Delivery.statuses.items()]
        items.insert(0, (_('Any'), None))
        return items

    #
    #  SearchEditor
    #

    def create_filters(self):
        self.set_text_field_columns([
            'tracking_code', 'transporter_name', 'recipient_name',
            'identifier_str'
        ])

        # Status
        statuses = [(desc, st) for st, desc in Delivery.statuses.items()]
        statuses.insert(0, (_('Any'), None))
        self.status_filter = ComboSearchFilter(_('With status:'), statuses)
        self.status_filter.select(None)
        self.add_filter(self.status_filter,
                        columns=['status'],
                        position=SearchFilterPosition.TOP)

    def get_editor_model(self, viewable):
        return viewable.delivery

    def get_columns(self):
        return [
            IdentifierColumn('identifier',
                             title=_('Sale #'),
                             order=Gtk.SortType.DESCENDING,
                             sorted=True),
            SearchColumn('status_str',
                         title=_('Status'),
                         data_type=str,
                         search_attribute='status',
                         valid_values=self._get_status_values()),
            Column('address_str',
                   title=_('Address'),
                   data_type=str,
                   expand=True,
                   ellipsize=Pango.EllipsizeMode.END),
            SearchColumn('tracking_code',
                         title=_('Tracking code'),
                         data_type=str),
            SearchColumn('transporter_name',
                         title=_('Transporter'),
                         data_type=str),
            SearchColumn('recipient_name', title=_('Recipient'),
                         data_type=str),
            SearchColumn('open_date',
                         title=_('Open date'),
                         data_type=datetime.date,
                         visible=False),
            SearchColumn('send_date',
                         title=_('Sent date'),
                         data_type=datetime.date,
                         visible=False),
            SearchColumn('receive_date',
                         title=_('Received date'),
                         data_type=datetime.date,
                         visible=False),
        ]
Example #13
0
class InventoryApp(ShellApp):

    # TODO: Change all widget.set_sensitive to self.set_sensitive([widget])
    app_title = _('Inventory')
    gladefile = "inventory"
    search_spec = Inventory
    search_labels = _('Matching:')
    report_table = InventoryReport

    #
    # Application
    #

    def create_actions(self):
        group = get_accels('app.inventory')
        actions = [
            # File
            ('NewInventory', None, _('Inventory...'),
             group.get('new_inventory'),
             _('Create a new inventory for product counting')),

            # Inventory
            ('Details', Gtk.STOCK_INFO, _('Details...'),
             group.get('inventory_details'),
             _('See details about this inventory')),
            ('CountingAction', Gtk.STOCK_INDEX, _('_Count...'),
             group.get('inventory_count'),
             _('Register the actual stock of products in the selected '
               'inventory')),
            ('AdjustAction', Gtk.STOCK_CONVERT, _('_Adjust...'),
             group.get('inventory_adjust'),
             _('Adjust the stock accordingly to the counting in the selected '
               'inventory')),
            ('Cancel', Gtk.STOCK_CANCEL, _('Cancel...'),
             group.get('inventory_cancel'),
             _('Cancel the selected inventory')),
            ('Export', Gtk.STOCK_SAVE, _('Export for external counting...'),
             None, _('Export the list of products for external counting')),
            ('PrintProductListing', Gtk.STOCK_PRINT,
             _('Print product listing...'), group.get('inventory_print'),
             _('Print the product listing for this inventory'))
        ]
        self.inventory_ui = self.add_ui_actions(actions)
        self.set_help_section(_("Inventory help"), 'app-inventory')

    def create_ui(self):
        self.window.add_new_items([self.NewInventory])
        self.window.add_print_items([self.PrintProductListing])
        self.window.add_export_items([self.Export])

    def activate(self, refresh=True):
        if refresh:
            # Avoid letting this sensitive if has-rows is never emitted
            self.refresh()
        self._update_widgets()

        self.search.focus_search_entry()

    def create_filters(self):
        # Disable string search right now, until we use a proper Viewable
        # for this application
        self.search.disable_search_entry()
        self.branch_filter = ComboSearchFilter(_('Show inventories at:'),
                                               self._get_branches_for_filter())

        current = api.get_current_branch(self.store)
        self.branch_filter.select(current.id)

        self.add_filter(self.branch_filter,
                        SearchFilterPosition.TOP,
                        columns=["branch_id"])

    def get_domain_options(self):
        options = [
            ('fa-info-circle-symbolic', _('Details'), 'inventory.Details',
             True),
            ('fa-list-ol-symbolic', _('Count'), 'inventory.CountingAction',
             True),
            ('fa-tasks-symbolic', _('Adjust'), 'inventory.AdjustAction', True),
            ('fa-ban-symbolic', _('Cancel'), 'inventory.Cancel', True),
        ]

        return options

    def get_columns(self):
        return [
            IdentifierColumn('identifier',
                             title=_('Inventory #'),
                             sorted=True,
                             order=Gtk.SortType.DESCENDING),
            SearchColumn('status_str',
                         title=_('Status'),
                         data_type=str,
                         width=100,
                         valid_values=self._get_status_values(),
                         search_attribute='status'),
            Column('branch.description',
                   title=_('Branch'),
                   data_type=str,
                   expand=True),
            SearchColumn('open_date',
                         title=_('Opened'),
                         long_title=_('Date Opened'),
                         data_type=datetime.date,
                         width=120),
            SearchColumn('close_date',
                         title=_('Closed'),
                         long_title=_('Date Closed'),
                         data_type=datetime.date,
                         width=120)
        ]

    #
    # Private API
    #

    def _get_status_values(self):
        values = [(v, k) for k, v in Inventory.statuses.items()]
        values.insert(0, (_("Any"), None))
        return values

    def _get_branches(self):
        return self.store.find(Branch)

    def _get_branches_for_filter(self):
        items = [(b.get_description(), b.id) for b in self._get_branches()]
        if not items:
            raise DatabaseInconsistency('You should have at least one '
                                        'branch on your database.'
                                        'Found zero')
        items.insert(0, [_('All branches'), None])
        return items

    def _update_widgets(self):
        has_open = False
        all_counted = False
        has_adjusted = False
        selected = self.results.get_selected()
        if selected:
            all_counted = selected.all_items_counted()
            has_open = selected.is_open()
            has_adjusted = selected.has_adjusted_items()

        self.set_sensitive([self.PrintProductListing, self.Details],
                           bool(selected))
        self.set_sensitive([self.Cancel], has_open and not has_adjusted)
        self.set_sensitive([self.NewInventory], self._can_open())
        self.set_sensitive([self.CountingAction], has_open and not all_counted)
        self.set_sensitive([self.AdjustAction], has_open and all_counted)

    def _can_open(self):
        branch = api.get_current_branch(self.store)
        if Inventory.has_open(self.store, branch):
            return False

        # It doesn't make sense to open an inventory if we don't have any stock
        return self.store.find(ProductStockItem, branch=branch).count() > 0

    def _open_inventory(self):
        with api.new_store() as store:
            rv = self.run_dialog(InventoryOpenEditor, store)

        if rv:
            self.refresh()
            self._update_widgets()

    def _cancel_inventory(self):
        if not yesno(_('Are you sure you want to cancel this inventory ?'),
                     Gtk.ResponseType.NO, _("Cancel inventory"),
                     _("Don't cancel")):
            return

        store = api.new_store()
        inventory = store.fetch(self.results.get_selected())
        inventory.cancel()
        store.commit()
        store.close()
        self.refresh()
        self._update_widgets()

    def _export_product_list(self):
        filename = save(_("Save products file"), self.get_toplevel(),
                        "%s.txt" % (_('products')))
        if not filename:
            return

        tables = [
            Sellable,
            Join(Storable, Storable.id == Sellable.id),
        ]
        sellables = self.store.using(*tables).find(Sellable)
        with open(filename, 'w') as fh:
            for sellable in sellables:
                # TODO: Add a dialog for the user to choose the format for
                # exporting
                fh.write('%-20s%s\n' %
                         (sellable.barcode, sellable.description))

    def _register_product_counting(self):
        store = api.new_store()
        inventory = store.fetch(self.results.get_selected())
        model = self.run_dialog(InventoryCountWizard, store, inventory)
        store.confirm(model)
        store.close()
        self.refresh()
        self._update_widgets()

    def _adjust_product_quantities(self):
        store = api.new_store()
        inventory = store.fetch(self.results.get_selected())
        model = self.run_dialog(InventoryAdjustmentEditor, store, inventory)
        store.confirm(model)
        store.close()
        self.refresh()
        self._update_widgets()

    def _update_filter_slave(self, slave):
        self.refresh()

    def _get_sellables_by_inventory(self, inventory):
        for item in inventory.get_items():
            yield item.product.sellable

    def _show_inventory_details(self):
        store = api.new_store()
        inventory = store.fetch(self.results.get_selected())
        self.run_dialog(InventoryDetailsDialog, store, inventory)
        store.close()

    #
    # Callbacks
    #

    def on_NewInventory__activate(self, action):
        self._open_inventory()

    def on_CountingAction__activate(self, action):
        self._register_product_counting()

    def on_AdjustAction__activate(self, action):
        self._adjust_product_quantities()

    def on_Details__activate(self, action):
        self._show_inventory_details()

    def on_results__selection_changed(self, results, product):
        self._update_widgets()

    def on_results__double_click(self, results, inventory):
        self._show_inventory_details()

    def on_Cancel__activate(self, widget):
        self._cancel_inventory()

    def on_Export__activate(self, widget):
        self._export_product_list()

    def on_PrintProductListing__activate(self, button):
        selected = self.results.get_selected()
        sellables = list(self._get_sellables_by_inventory(selected))
        if not sellables:
            warning(_("No products found in the inventory."))
            return
        self.print_report(ProductCountingReport, sellables)