Вы не вошли - Войти
< назад

Пример отчета Остатки товаров

function run() {
       var date = context.inParams.Date;
       var lines = context.inParams.Lines || [];
       var linesList = context.inParams.LinesList || [];
       var showGoodGroups = context.inParams.ShowGoodGroups;
       var goods = context.inParams.Goods || [];
       var subdivs = context.inParams.Subdivs || [];
       var groupsCount = context.inParams.GroupsCount;
       
       var groupFields = [];
       linesList.forEach((line) => {
              switch(line) {
                     case 'Склады':
                            if (lines.indexOf(line) >= 0) {
                                   groupFields.push(new TreeGroup('Склад'));
                            }
                            break;
                     case 'Товары':
                            if (lines.indexOf(line) >= 0) {
                                   groupFields.push(new TreeGroup({ fieldName: 'Товар', addGroups: showGoodGroups }));
                            } else if (showGoodGroups) {
                                   groupFields.push(new TreeGroup({fieldName: 'Товар', addGroups: true, onlyGroups: true, maxGroupLevel: groupsCount }));
                            }
                            break;
              }
       });
       
       var criteria = createRestQuery("Остатки товаров")
              .sum('Количество')
              .sum('Сумма')
              .onDate(date);
       
       groupFields.forEach((group) => {
              criteria.select(group.fieldName);
       });
       
       if (goods.length > 0)
              criteria.whereIn('Товар', getAllCodes('Товары', goods));

       if (subdivs.length > 0)
              criteria.whereIn('Склад', getAllCodes('Склады', subdivs));
       
       var result = context.repository.executeQuery(criteria);
       if (groupFields.length > 0)
              result = result.buildTree(groupFields, ['Количество', 'Сумма']);
       
       var rep = createReport('ОстаткиТоваров.tcrp');
       rep.addSection("Шапка");
       rep.setParameter("Период", "на: " + toString(date));
       if (subdivs.length > 0)
              rep.setParameter("Склады", "По складам: " + getDictNames("Склады", subdivs));
       if (goods.length > 0)
              rep.setParameter("Товары", "По товарам: " + getDictNames("Товары", goods));
       rep.addSection("Таблица");
       
       result.processLevels((r, level, group) =>
                     {       {
                            if              if (group == null)
                                   return;                     return;

                            var              var property = r.getProperty(group.fieldName);
                            rep.addSection(level.toString());              rep.addSection(level.toString());
                            var              var typeName = property.field.objName;
                            rep.setParameter("Type",              rep.setParameter("Type", typeName);
                            rep.setParameter("Code",              rep.setParameter("Code", r[group.fieldName]);
                            rep.setParameter("Код",              rep.setParameter("Код", r[group.fieldName]);
                            rep.setParameter("Наим",              rep.setParameter("Наим", r.getString(group.fieldName));
                            if              if (typeName == "Товары")
                                   rep.setParameter("Единица",                     rep.setParameter("Единица", property.getString("Единица измерения"));

                            var
              var qnt = r.Количество;
                            var              var amount = r.Сумма;

                            rep.setParameter("Кол",              rep.setParameter("Кол", qnt);
                            rep.setParameter("Цена",              rep.setParameter("Цена", qnt != 0 ? amount / qnt : 0);
                            rep.setParameter("Сум",              rep.setParameter("Сум", amount);
                     });       });
       
       result.toBegin();
       result.read();
       rep.addSection("Подвал");
       rep.setParameter("Кол", result.Количество);
       rep.setParameter("Сум", result.Сумма);
}