示例#1
0
        public async Task LoadInvoices(string carId)
        {
            var query = new QyeryBuilder();

            query.SetSortByField(InvoiceRecord.SEQ);
            if (string.IsNullOrEmpty(carId))
            {
                query.FilterByFormula = "{Date} >= TODAY()";
            }
            else
            {
                query.FilterByFormula = $"And({{Date}} = TODAY(), {{Car}}=\"{carId}\")";
            }

            //var records = await InvoicesTable.ListRecords(sortField: InvoiceRecord.SEQ);
            var result = await InvoicesTable.List(query);

            var records = result?.Records;

            if (records == null)
            {
                Debug.Print("InvoicesTable.List() returned no records");
                return;
            }
            var invoices = AppScope.Instance.Invoices;
            int ord      = 0;

            foreach (var rec in records)
            {
                var customer = AppScope.GetCustomer(rec.Customer);
                if (customer == null)
                {
                    Debug.Print($"## AirStorage.LoadInvoices(): Unknown Customer {Dw.ToString(rec.Customer)}");
                    continue;
                }

                var invoice = new Invoice {
                    RecordId = rec.Id,
                    Seq      = rec.Seq,
                    Ordinal  = ++ord,
                    Date     = rec.Date,
                    CarId    = rec.Car,
                    Number   = rec.Number,
                    Customer = customer,
                    Notes    = rec.Notes
                };

                var list = await ArticlesTable.FilterRecords($"{{{ArticleRecord.INVOICE_SEQ}}} = {invoice.Seq}");

                foreach (var art in list.Records)
                {
                    var produce = AppScope.GetProduce(art.Produce);
                    if (produce == null)
                    {
                        Debug.Print($"## AirStorage.LoadInvoices(): Unknown Produce {Dw.ToString(art.Produce)}");
                        continue;
                    }

                    var article = new Article {
                        RecordId   = art.Id,
                        Produce    = produce,
                        Quantity   = art.Quantity,
                        Unit       = art.Unit,
                        UnitPrice  = art.UnitPrice,
                        TotalPrice = art.TotalPrice,
                        Note       = art.Note
                    };

                    invoice.Articles.Add(article);
                }

                invoices.Add(invoice);
            }
        }