|
1 | 1 | import {CommerceMlAbstractParser, CommerceMlCollectRule} from './abstract-parser'; |
| 2 | +import { |
| 3 | + Catalog, |
| 4 | + Classifier, |
| 5 | + ClassifierGroup, |
| 6 | + ClassifierProperty, |
| 7 | + CommercialInformation, |
| 8 | + Counterparty, DictionaryValue, Product, |
| 9 | + PropertyValue, RequisiteValue |
| 10 | +} from './types'; |
2 | 11 |
|
3 | 12 | export class CommerceMlImportParser extends CommerceMlAbstractParser { |
| 13 | + /** |
| 14 | + * Parses commercial information schemaVersion and creationTimestamp attributes. |
| 15 | + * @param callback |
| 16 | + */ |
| 17 | + public onCommercialInformation(callback: (commercialInformation: CommercialInformation) => void): void { |
| 18 | + this.parser.on('commercialInformation', (data: any) => { |
| 19 | + const commercialInformation: CommercialInformation = { |
| 20 | + schemaVersion: data.КоммерческаяИнформация._ВерсияСхемы, |
| 21 | + creationTimestamp: data.КоммерческаяИнформация._ДатаФормирования |
| 22 | + }; |
| 23 | + |
| 24 | + callback(commercialInformation); |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Parses classifier block header without details. |
| 30 | + * @param callback |
| 31 | + */ |
| 32 | + public onClassifier(callback: (classifier: Classifier) => void): void { |
| 33 | + this.parser.on('classifier', (data: any) => { |
| 34 | + const classifierXml = data.Классификатор; |
| 35 | + const classifier: Classifier = { |
| 36 | + id: classifierXml.Ид, |
| 37 | + name: classifierXml.Наименование, |
| 38 | + owner: this.parseCounterpartyXmlData(classifierXml.Владелец) |
| 39 | + }; |
| 40 | + |
| 41 | + callback(classifier); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Parses classifier groups. |
| 47 | + * @param callback |
| 48 | + */ |
| 49 | + public onClassifierGroup(callback: (classifierGroup: ClassifierGroup) => void): void { |
| 50 | + const processGroup = (groupData: any): ClassifierGroup => { |
| 51 | + const result: ClassifierGroup = { |
| 52 | + id: groupData.Ид, |
| 53 | + name: groupData.Наименование |
| 54 | + }; |
| 55 | + |
| 56 | + const children: ClassifierGroup[] = []; |
| 57 | + if (groupData.Группы?.Группа?.length > 0) { |
| 58 | + for (const child of groupData.Группы.Группа) { |
| 59 | + children.push(processGroup(child)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (children.length > 0) { |
| 64 | + result.groups = children; |
| 65 | + } |
| 66 | + |
| 67 | + return result; |
| 68 | + }; |
| 69 | + |
| 70 | + this.parser.on('classifierGroup', (data: any) => { |
| 71 | + const classifierGroupXml = data.Группа; |
| 72 | + const classifierGroup: ClassifierGroup = processGroup(classifierGroupXml); |
| 73 | + callback(classifierGroup); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Parses classifier properties. |
| 79 | + * @param callback |
| 80 | + */ |
| 81 | + public onClassifierProperty(callback: (classifierProperty: ClassifierProperty) => void): void { |
| 82 | + this.parser.on('classifierProperty', (data: any) => { |
| 83 | + const propertyXml = data.Свойство; |
| 84 | + |
| 85 | + const classifierProperty: ClassifierProperty = { |
| 86 | + id: propertyXml.Ид, |
| 87 | + name: propertyXml.Наименование, |
| 88 | + type: propertyXml.ТипЗначений |
| 89 | + }; |
| 90 | + |
| 91 | + if (propertyXml.ВариантыЗначений?.Справочник?.length > 0) { |
| 92 | + classifierProperty.dictionaryValues = []; |
| 93 | + for (const dictionaryValue of propertyXml.ВариантыЗначений.Справочник) { |
| 94 | + classifierProperty.dictionaryValues.push({ |
| 95 | + id: dictionaryValue.ИдЗначения, |
| 96 | + value: dictionaryValue.Значение |
| 97 | + } as DictionaryValue); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + callback(classifierProperty); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Parses catalog header without details. |
| 107 | + * @param callback |
| 108 | + */ |
| 109 | + public onCatalog(callback: (catalog: Catalog) => void) { |
| 110 | + this.parser.on('catalog', (data: any) => { |
| 111 | + const catalogXml = data.Каталог; |
| 112 | + const catalog: Catalog = { |
| 113 | + id: catalogXml.Ид, |
| 114 | + classifierId: catalogXml.ИдКлассификатора, |
| 115 | + name: catalogXml.Наименование, |
| 116 | + owner: this.parseCounterpartyXmlData(catalogXml.Владелец), |
| 117 | + products: [] |
| 118 | + }; |
| 119 | + |
| 120 | + callback(catalog); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Parses catalog products. |
| 126 | + * @param callback |
| 127 | + */ |
| 128 | + public onProduct(callback: (product: Product) => void) { |
| 129 | + this.parser.on('product', (data: any) => { |
| 130 | + const productXml = data.Товар; |
| 131 | + const product: Product = { |
| 132 | + id: productXml.Ид, |
| 133 | + article: productXml.Артикул, |
| 134 | + name: productXml.Наименование, |
| 135 | + baseMeasurementUnit: { |
| 136 | + code: productXml.БазоваяЕдиница._Код, |
| 137 | + fullName: productXml.БазоваяЕдиница._НаименованиеПолное, |
| 138 | + acronym: productXml.БазоваяЕдиница._МеждународноеСокращение |
| 139 | + }, |
| 140 | + group: productXml.Группы.Ид, |
| 141 | + description: productXml.Описание |
| 142 | + }; |
| 143 | + |
| 144 | + if (productXml.СтавкиНалогов?.СтавкаНалога) { |
| 145 | + if (Array.isArray(productXml.СтавкиНалогов?.СтавкаНалога)) { |
| 146 | + product.taxRates = []; |
| 147 | + for (const taxRateXml of productXml.СтавкиНалогов?.СтавкаНалога) { |
| 148 | + product.taxRates.push({ |
| 149 | + name: taxRateXml.Наименование, |
| 150 | + rate: taxRateXml.Ставка |
| 151 | + }); |
| 152 | + } |
| 153 | + } else { |
| 154 | + product.taxRates = [{ |
| 155 | + name: productXml.СтавкиНалогов.СтавкаНалога.Наименование, |
| 156 | + rate: productXml.СтавкиНалогов.СтавкаНалога.Ставка |
| 157 | + }]; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + if (productXml.Штрихкод) { |
| 162 | + product.barcode = productXml.Штрихкод; |
| 163 | + } |
| 164 | + |
| 165 | + if (productXml.Изготовитель) { |
| 166 | + product.manufacturer = { |
| 167 | + id: productXml.Изготовитель.Ид, |
| 168 | + name: productXml.Изготовитель.Наименование |
| 169 | + }; |
| 170 | + } |
| 171 | + |
| 172 | + if (productXml.ЗначенияСвойств?.ЗначенияСвойства?.length > 0) { |
| 173 | + product.propertyValues = []; |
| 174 | + for (const propertyValue of productXml.ЗначенияСвойств.ЗначенияСвойства) { |
| 175 | + if (Array.isArray(propertyValue.Значение)) { |
| 176 | + product.propertyValues.push({ |
| 177 | + id: propertyValue.Ид, |
| 178 | + values: propertyValue.Значение |
| 179 | + } as PropertyValue); |
| 180 | + } else { |
| 181 | + product.propertyValues.push({ |
| 182 | + id: propertyValue.Ид, |
| 183 | + values: [propertyValue.Значение] |
| 184 | + } as PropertyValue); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if (productXml.ЗначенияРеквизитов?.ЗначениеРеквизита?.length > 0) { |
| 190 | + product.requisiteValues = []; |
| 191 | + for (const requisiteValue of productXml.ЗначенияРеквизитов.ЗначениеРеквизита ?? []) { |
| 192 | + if (Array.isArray(requisiteValue.Значение)) { |
| 193 | + product.requisiteValues.push({ |
| 194 | + name: requisiteValue.Наименование, |
| 195 | + values: requisiteValue.Значение |
| 196 | + } as RequisiteValue); |
| 197 | + } else { |
| 198 | + product.requisiteValues.push({ |
| 199 | + name: requisiteValue.Наименование, |
| 200 | + values: [requisiteValue.Значение] |
| 201 | + } as RequisiteValue); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + callback(product); |
| 207 | + }); |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Helper method to parse counterparty XML data. |
| 212 | + * @param xmlData |
| 213 | + */ |
| 214 | + protected parseCounterpartyXmlData(xmlData: any): Counterparty { |
| 215 | + const counterparty: Counterparty = { |
| 216 | + id: xmlData.Ид, |
| 217 | + name: xmlData.Наименование |
| 218 | + }; |
| 219 | + |
| 220 | + // Detect company info or person info |
| 221 | + if (xmlData.ОфициальноеНаименование) { |
| 222 | + counterparty.companyInfo = { |
| 223 | + officialName: xmlData.ОфициальноеНаименование, |
| 224 | + inn: xmlData.ИНН, |
| 225 | + kpp: xmlData.КПП, |
| 226 | + okpo: xmlData.ОКПО |
| 227 | + }; |
| 228 | + } else { |
| 229 | + counterparty.personInfo = { |
| 230 | + fullName: xmlData.ПолноеНаименование |
| 231 | + }; |
| 232 | + } |
| 233 | + |
| 234 | + return counterparty; |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Parser rules. |
| 239 | + */ |
4 | 240 | protected getCollectRules(): {[key: string]: CommerceMlCollectRule} { |
5 | 241 | return { |
6 | | - commercialInfo: { |
| 242 | + commercialInformation: { |
7 | 243 | start: ['КоммерческаяИнформация'] |
8 | 244 | }, |
9 | 245 | classifier: { |
|
0 commit comments