Example #1
0
        async void CreateMandates(IList debtors)
        {
            var mandateCache = api.GetCache(typeof(Uniconta.DataModel.DebtorPaymentMandate)) ?? await api.LoadCache(typeof(Uniconta.DataModel.DebtorPaymentMandate));

            CWDirectDebit cwwin = new CWDirectDebit(api, string.Format(Uniconta.ClientTools.Localization.lookup("CreateOBJ"), Uniconta.ClientTools.Localization.lookup("Mandates")), true);

            cwwin.Closing += delegate
            {
                if (cwwin.DialogResult == true)
                {
                    var lstDebtors = debtors.Cast <Uniconta.ClientTools.DataModel.DebtorClient>();
                    var lstInsert  = new List <Uniconta.DataModel.DebtorPaymentMandate>();
                    var lstUpdate  = new List <Uniconta.DataModel.Debtor>();

                    bool mandateIsDebtor = cwwin.PaymentFormat._UseDebtorAccAsMandate;

                    foreach (var rec in lstDebtors)
                    {
                        var mandate = (Uniconta.DataModel.DebtorPaymentMandate)mandateCache?.Get(rec._Account);
                        if (mandate != null)
                        {
                            continue;
                        }

                        var newMandate = new Uniconta.DataModel.DebtorPaymentMandate();
                        newMandate.SetMaster(rec);
                        newMandate._AltMandateId = mandateIsDebtor ? rec._Account : null;
                        newMandate._Scheme       = cwwin.directDebitScheme;
                        newMandate._StatusInfo   = string.Format("({0}) {1}", Uniconta.DirectDebitPayment.Common.GetTimeStamp(), Uniconta.ClientTools.Localization.lookup("Created"));;
                        lstInsert.Add(newMandate);

                        if (rec.PaymentFormat != cwwin.PaymentFormat._Format)
                        {
                            rec.PaymentFormat = cwwin.PaymentFormat._Format;
                            lstUpdate.Add(rec);
                        }
                    }

                    if (lstInsert.Count > 0)
                    {
                        api.InsertNoResponse(lstInsert);
                        UnicontaMessageBox.Show(string.Format(Uniconta.ClientTools.Localization.lookup("RecordsUpdated"), lstInsert.Count, Uniconta.ClientTools.Localization.lookup("Mandates")), Uniconta.ClientTools.Localization.lookup("Information"));
                    }

                    if (lstUpdate.Count > 0)
                    {
                        api.UpdateNoResponse(lstUpdate);
                    }
                }
            };
            cwwin.Show();
        }