github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/GUI/messages/destinataires.go (about)

     1  package messages
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/benoitkugler/goACVE/client/GUI/basic"
     7  	"github.com/benoitkugler/goACVE/client/GUI/fields"
     8  	"github.com/benoitkugler/goACVE/client/GUI/lists"
     9  	"github.com/benoitkugler/goACVE/client/controllers"
    10  	dm "github.com/benoitkugler/goACVE/server/core/datamodel"
    11  	rd "github.com/benoitkugler/goACVE/server/core/rawdata"
    12  	"github.com/therecipe/qt/widgets"
    13  )
    14  
    15  type facturesSelector struct {
    16  	*widgets.QFrame
    17  
    18  	list lists.Table
    19  
    20  	ct *controllers.SuiviDossiers
    21  }
    22  
    23  func newFacturesSelector(ct *controllers.SuiviDossiers) *facturesSelector {
    24  	f := &facturesSelector{QFrame: basic.Frame(), ct: ct}
    25  	f.list = lists.Table{
    26  		Liste: lists.Liste{
    27  			Headers: ct.Header,
    28  			Title:   "Dossiers à contacter",
    29  			OnDelete: func(_ rd.Item, index int) {
    30  				m := f.list.Model()
    31  				m.SetData(controllers.RemoveItem(m.GetData(), index))
    32  			},
    33  		},
    34  	}
    35  	f.list.Init()
    36  	f.list.HorizontalHeader().SetMinimumSectionSize(200)
    37  
    38  	add := fields.NewBoutonIdFacture(true, ct.Base)
    39  	add.DataChanged = func() {
    40  		f.addFacture(rd.FromIId(add.GetData()))
    41  	}
    42  
    43  	copyList := basic.Button("Ajouter la liste principale actuelle")
    44  	copyList.ConnectClicked(f.copyList)
    45  
    46  	layout := widgets.NewQHBoxLayout2(f)
    47  
    48  	layoutLeft := widgets.NewQVBoxLayout()
    49  	layoutLeft.AddWidget(basic.Label("Ajouter un dossier :"), 1, 0)
    50  	layoutLeft.AddWidget(add, 1, 0)
    51  	layoutLeft.AddStretch(3)
    52  	layoutLeft.AddWidget(copyList, 1, 0)
    53  
    54  	layout.AddLayout(layoutLeft, 1)
    55  	layout.AddWidget(f.list, 3, 0)
    56  	f.SetMinimumHeight(400)
    57  
    58  	return f
    59  }
    60  
    61  func (f *facturesSelector) addFacture(idF rd.OptionnalId) {
    62  	if idF.IsNil() {
    63  		return
    64  	}
    65  	fac := f.ct.Base.NewFacture(idF.Int64).AsItem(nil, nil, nil)
    66  	l := f.list.Model().GetData()
    67  	if !controllers.HasId(l, fac.Id) {
    68  		f.list.Model().SetData(append(l, fac))
    69  	}
    70  }
    71  
    72  func (f *facturesSelector) copyList(_ bool) {
    73  	ids := make(map[rd.IId]rd.Item)
    74  	for _, item := range f.list.Model().GetData() {
    75  		ids[item.Id] = item
    76  	}
    77  	for _, item := range f.ct.Liste {
    78  		ids[item.Id] = item
    79  	}
    80  	// fusion
    81  	out := make(rd.Table, 0, len(ids))
    82  	for _, item := range ids {
    83  		out = append(out, item)
    84  	}
    85  	dm.SortCriteres(out, f.ct.Etat.CriteresTri)
    86  	f.list.Model().SetData(out)
    87  }
    88  
    89  // SelectFactures affiche un dialogue pour sélectionner manuellement
    90  // une liste de factures. La liste vide est renvoyé en cas d'annulation
    91  func SelectFactures(ct *controllers.SuiviDossiers) rd.Ids {
    92  	d := basic.Dialog("Sélection")
    93  	fs := newFacturesSelector(ct)
    94  	valid := basic.Button("Valider")
    95  	valid.SetObjectName(basic.ONAdd)
    96  	valid.ConnectClicked(func(_ bool) {
    97  		d.Accept()
    98  	})
    99  
   100  	d.Layout().AddWidget(fs)
   101  	d.Layout().AddWidget(valid)
   102  	if d.Exec() > 0 {
   103  		return controllers.ToIds(fs.list.Model().GetData())
   104  	}
   105  	return nil
   106  }
   107  
   108  // ---------- Sélection destinataire message documents --------------
   109  
   110  type choixCampDocument struct {
   111  	camps  fields.Camp
   112  	resend *widgets.QCheckBox
   113  	valid  *widgets.QPushButton
   114  
   115  	base *dm.BaseLocale
   116  }
   117  
   118  func newChoixCampDocument(base *dm.BaseLocale) choixCampDocument {
   119  	var c choixCampDocument
   120  	c.base = base
   121  	c.camps = fields.NewCamp(true, base, false)
   122  	c.camps.ShowTerminated(false)
   123  	c.camps.ConnectCurrentIndexChanged(func(_ int) { c.onCampChange() })
   124  
   125  	c.resend = widgets.NewQCheckBox2("Envoyer une deuxième fois", nil)
   126  	c.resend.SetToolTip("Pour les responsables déjà contacté, envoie un message à nouveau")
   127  
   128  	c.valid = basic.Button("Envoyer")
   129  	c.onCampChange()
   130  	return c
   131  }
   132  
   133  func (c choixCampDocument) onCampChange() {
   134  	camp := c.camps.GetData()
   135  	c.valid.SetEnabled(camp != nil)
   136  	if camp != nil {
   137  		facs := c.base.GetFacturesSendDocuments(camp.Id)
   138  		if L := len(facs); L > 0 {
   139  			c.resend.SetText(fmt.Sprintf("Envoyer une deuxième fois (pour %d dossier(s))", L))
   140  			c.resend.SetEnabled(true)
   141  			return
   142  		}
   143  	}
   144  	c.resend.SetText("-")
   145  	c.resend.SetEnabled(false)
   146  }
   147  
   148  func (c choixCampDocument) getData() (idCamp int64, resend bool) {
   149  	return c.camps.GetData().Id, c.resend.IsChecked()
   150  }
   151  
   152  func SelectCamp(base *dm.BaseLocale) (idCamp int64, resend, keep bool) {
   153  	d := basic.Dialog2("Choix du séjour")
   154  	form := newChoixCampDocument(base)
   155  	form.valid.ConnectClicked(func(_ bool) {
   156  		idCamp, resend = form.getData()
   157  		d.Accept()
   158  	})
   159  
   160  	layout := widgets.NewQFormLayout(d)
   161  	layout.AddRow3("Choix du séjour", form.camps)
   162  	layout.AddRow5(form.resend)
   163  	layout.AddRow5(form.valid)
   164  
   165  	if d.Exec() > 0 {
   166  		return idCamp, resend, true
   167  	}
   168  	return idCamp, resend, false
   169  }