249 lines
10 KiB
Python
249 lines
10 KiB
Python
# SPDX-License-Identifier: WTFPL
|
|
|
|
import os
|
|
from gi.repository import Gtk
|
|
|
|
from .view import View
|
|
from yms import gtkutils, combobox, treeview
|
|
from yms.lookup import Dropdown
|
|
from yms.model import company
|
|
|
|
class CompaniesView(View):
|
|
|
|
def __init__(self, context):
|
|
super().__init__(context, _("Companies"), "companies.ui")
|
|
self.context = context
|
|
|
|
self.comp = company.Company(context.db) # current record
|
|
self.newrecord = False
|
|
|
|
# company types
|
|
self.dd_comptype = self.context.lookup.dropdown(Dropdown.COMPANY_TYPE)
|
|
combobox.load_from_dict(self.combo['flt_type'], {-2: _("─── all ───"), -1: _("─── none ───")} | self.dd_comptype, -2)
|
|
combobox.load_from_dict(self.combo['comptype'], self.dd_comptype, company.SUPPLIER)
|
|
combobox.load_from_dict(self.combo['comptype2'], {-1: _("─── none ───")} | self.dd_comptype)
|
|
|
|
self.dd_flags = context.lookup.enum('company.flags')
|
|
|
|
# companies
|
|
self.lst_companies = Gtk.ListStore(int, str, str, str)
|
|
self.load_list()
|
|
|
|
treeview.add_column(self.tree['companies'], _('ID'), 0)
|
|
treeview.add_column(self.tree['companies'], _('Name'), 1)
|
|
treeview.add_column(self.tree['companies'], _('Type'), 2)
|
|
treeview.add_column(self.tree['companies'], _('Remarks'), 3)
|
|
self.tree['companies'].set_model(self.lst_companies)
|
|
|
|
# subscribe to data change events
|
|
self.context.subscribe("dd_comptype_modified", self.comptype_modified)
|
|
|
|
def get_list_row(self):
|
|
return (
|
|
self.comp.compid,
|
|
self.comp.compname,
|
|
self.dd_comptype.get(self.comp.comptype, ''),
|
|
self.comp.remarks
|
|
)
|
|
|
|
def add_row(self):
|
|
treeiter = self.lst_companies.append(self.get_list_row())
|
|
self.tree['companies'].get_selection().select_iter(treeiter)
|
|
|
|
def refresh_row(self):
|
|
treeview.set_row_values(self.tree['companies'], self.get_list_row())
|
|
|
|
def load_list(self):
|
|
selected_id = treeview.get_value(self.tree['companies'])
|
|
self.lst_companies.clear()
|
|
sql = (
|
|
"SELECT compid, compname, comptype, remarks "
|
|
"FROM company "
|
|
"WHERE compid >= 0 "
|
|
"ORDER BY compname"
|
|
)
|
|
self.db.cur.execute(sql)
|
|
selection = self.tree['companies'].get_selection()
|
|
for row in self.db.cur.fetchall():
|
|
treeiter = self.lst_companies.append((row[0], row[1], self.dd_comptype[row[2]], row[3]))
|
|
if row[0] == selected_id:
|
|
# restore previous selected row
|
|
selection.select_iter(treeiter)
|
|
|
|
def comptype_modified(self, eventtype, data):
|
|
self.dd_comptype[data[0]] = data[1]
|
|
lst1 = self.combo['flt_type'].get_model()
|
|
lst2 = self.combo['comptype'].get_model()
|
|
lst3 = self.combo['comptype2'].get_model()
|
|
if eventtype == self.context.EVENT_UPD:
|
|
for l in (lst1, lst2, lst3):
|
|
for row in l:
|
|
if row[0] == data[0]:
|
|
row[1] = data[1]
|
|
return
|
|
elif eventtype == self.context.EVENT_ADD:
|
|
lst1.append(data)
|
|
lst2.append(data)
|
|
lst3.append(data)
|
|
else:
|
|
# not implemented
|
|
pass
|
|
|
|
def clear_record(self):
|
|
self.comp.clear()
|
|
self.entry['compname'].set_text('')
|
|
self.entry['shortname'].set_text('')
|
|
self.entry['street'].set_text('')
|
|
self.entry['zip'].set_text('')
|
|
self.entry['city'].set_text('')
|
|
self.entry['country'].set_text('')
|
|
self.entry['contact'].set_text('')
|
|
self.entry['phone'].set_text('')
|
|
self.entry['email'].set_text('')
|
|
self.entry['web'].set_text('')
|
|
self.entry['customerno'].set_text('')
|
|
self.entry['contractno'].set_text('')
|
|
combobox.activate_row(self.combo['comptype'], company.SUPPLIER)
|
|
combobox.activate_row(self.combo['comptype2'], -1)
|
|
self.entry['compname'].grab_focus()
|
|
|
|
def display_record(self):
|
|
self.label['name'].set_text(self.comp.compname)
|
|
self.label['shortname'].set_text(self.comp.shortname or '')
|
|
self.label['type'].set_text(self.dd_comptype.get(self.comp.comptype, ''))
|
|
self.label['type2'].set_text(self.dd_comptype.get(self.comp.comptype2, ''))
|
|
self.label['street'].set_text(self.comp.street or '')
|
|
self.label['zip'].set_text(self.comp.zip or '')
|
|
self.label['city'].set_text(self.comp.city or '')
|
|
self.label['country'].set_text(self.comp.country or '')
|
|
self.label['contact'].set_text(self.comp.contact or '')
|
|
self.label['phone'].set_text(self.comp.phone or '')
|
|
self.label['email'].set_text(self.comp.email or '')
|
|
self.label['web'].set_text(self.comp.web or '')
|
|
self.label['customerno'].set_text(self.comp.customerno or '')
|
|
self.label['contractno'].set_text(self.comp.contractno or '')
|
|
self.label['flags'].set_text(self.comp.flags or '')
|
|
# TODO translation
|
|
self.label['remarks'].set_text(self.comp.remarks or '')
|
|
|
|
def edit_record(self):
|
|
self.entry['compname'].set_text(self.comp.compname)
|
|
combobox.activate_row(self.combo['comptype'], self.comp.comptype)
|
|
combobox.activate_row(self.combo['comptype2'], self.comp.comptype2 or -1)
|
|
self.entry['shortname'].set_text(self.comp.shortname or '')
|
|
self.entry['street'].set_text(self.comp.street or '')
|
|
self.entry['zip'].set_text(self.comp.zip or '')
|
|
self.entry['city'].set_text(self.comp.city or '')
|
|
self.entry['country'].set_text(self.comp.country or '')
|
|
self.entry['contact'].set_text(self.comp.contact or '')
|
|
self.entry['phone'].set_text(self.comp.phone or '')
|
|
self.entry['email'].set_text(self.comp.email or '')
|
|
self.entry['web'].set_text(self.comp.web or '')
|
|
self.entry['customerno'].set_text(self.comp.customerno or '')
|
|
self.entry['contractno'].set_text(self.comp.contractno or '')
|
|
flags = (self.comp.flags or '').split(',')
|
|
for flag in self.dd_flags:
|
|
self.check[flag].set_active(flag in flags)
|
|
self.text['remarks'].get_buffer().set_text(self.comp.remarks or '')
|
|
self.entry['compname'].grab_focus()
|
|
|
|
def save_record(self):
|
|
# fields must have been validated beforehand
|
|
self.comp.compname = self.entry['compname'].get_text()
|
|
self.comp.comptype = combobox.get_list_value(self.combo['comptype'])
|
|
self.comp.comptype2 = combobox.get_list_value(self.combo['comptype2'])
|
|
if self.comp.comptype2 < 0:
|
|
self.comp.comptype2 = None
|
|
self.comp.shortname = self.entry['shortname'].get_text()
|
|
self.comp.street = self.entry['street'].get_text()
|
|
self.comp.zip = self.entry['zip'].get_text()
|
|
self.comp.city = self.entry['city'].get_text()
|
|
self.comp.country = self.entry['country'].get_text()
|
|
self.comp.contact = self.entry['contact'].get_text()
|
|
self.comp.phone = self.entry['phone'].get_text()
|
|
self.comp.email = self.entry['email'].get_text()
|
|
self.comp.web = self.entry['web'].get_text()
|
|
self.comp.customerno = self.entry['customerno'].get_text()
|
|
self.comp.contractno = self.entry['contractno'].get_text()
|
|
self.comp.flags = self.get_checks(('historic', 'deleted'))
|
|
self.comp.remarks = gtkutils.get_text_contents(self.text['remarks'])
|
|
|
|
# buttons on list page
|
|
def on_but_new_clicked(self, widget):
|
|
self.newrecord = True
|
|
self.label['title_edit'].set_text(_("Add new Company"))
|
|
self.clear_record()
|
|
self.topwidget.set_visible_child(self.box['comp_edit'])
|
|
|
|
def on_but_edit_clicked(self, widget):
|
|
self.newrecord = False
|
|
compid = treeview.get_value(self.tree['companies'], 0)
|
|
if not compid:
|
|
return
|
|
if not self.record_lock('company', compid):
|
|
return
|
|
self.comp.load(compid)
|
|
self.edit_record()
|
|
self.label['title_edit'].set_text(_("Edit Company"))
|
|
|
|
self.topwidget.set_visible_child(self.box['comp_edit'])
|
|
|
|
def on_but_detail_clicked(self, widget):
|
|
compid = treeview.get_value(self.tree['companies'], 0)
|
|
if not compid:
|
|
return
|
|
self.comp.load(compid)
|
|
self.display_record()
|
|
self.topwidget.set_visible_child(self.box['comp_detail'])
|
|
|
|
def on_tvw_companies_row_activated(self, widget, path, column):
|
|
compid = treeview.get_value(widget, 0)
|
|
self.comp.load(compid)
|
|
self.display_record()
|
|
self.topwidget.set_visible_child(self.box['comp_detail'])
|
|
|
|
# buttons on detail page
|
|
# edit2 uses edit callback
|
|
def on_but_close_clicked(self, widget):
|
|
self.topwidget.set_visible_child(self.box['companies'])
|
|
|
|
def on_but_delete_clicked(self, widget):
|
|
answer = gtkutils.questionbox(
|
|
_("Delete company!\n"
|
|
"Are you sure?"), title="YMS"
|
|
)
|
|
if answer == False:
|
|
return
|
|
self.topwidget.set_visible_child(self.box['companies'])
|
|
|
|
# buttons on edit page
|
|
def on_but_save_clicked(self, widget):
|
|
# check mandatory fields:
|
|
compname = self.entry['compname'].get_text().strip()
|
|
# TODO type
|
|
if not compname:
|
|
# TODO error marker
|
|
return
|
|
self.save_record()
|
|
print(self.comp)
|
|
if not self.comp.save():
|
|
pass
|
|
self.record_unlock()
|
|
if self.newrecord:
|
|
self.add_row()
|
|
self.context.publish("company_modified", self.context.EVENT_ADD,
|
|
(self.comp.pk_value, self.comp.comptype, self.comp.compname)
|
|
)
|
|
else:
|
|
self.refresh_row()
|
|
self.context.publish("company_modified", self.context.EVENT_UPD,
|
|
(self.comp.pk_value, self.comp.comptype, self.comp.compname)
|
|
)
|
|
self.newrecord = False
|
|
self.display_record()
|
|
self.topwidget.set_visible_child(self.box['comp_detail'])
|
|
|
|
def on_but_cancel_clicked(self, widget):
|
|
self.record_unlock()
|
|
self.topwidget.set_visible_child(self.box['companies'])
|