import moment from "moment" function xmlContent(content){ if(content != null){ return { "$t" : content }; } return null; } export class RentalReservationConverter { shopify2Wintersteiger(data){ const reservations = data.map(this.populateWintersteigerReservation.bind(this)); const xmlData = { reservations : { version : "2.0", origin : "Shopify", reservation : reservations } } return xmlData; } wintersteiger2Shopify(data){ const jsonData = data.map(function(orderData){ return orderData; }); return jsonData; } populateWintersteigerReservation(order) { var self = this; const peopleData = self.getPeopleData(order); const rentalItems = order.line_items.filter(this.filterRentalItems); const peopleItems = self.collatePeopleItems(rentalItems); const reservationStart = this.getProperty("reservationStart", order.note_attributes); const reservationEnd = this.getProperty("reservationEnd", order.note_attributes); const datefrom = moment(reservationStart, "MM/DD/YYY").format("YYYY-MM-DD"); const dateto = moment(reservationEnd, "MM/DD/YYY").format("YYYY-MM-DD"); const datecreated = moment(order.created_at).format("YYYY-MM-DDThh:mm:ss"); const datelastupdated = moment(order.updated_at).format("YYYY-MM-DDThh:mm:ss"); const reservation = { reservationextid : xmlContent(order.id), datefrom : xmlContent(datefrom), dateto : xmlContent(dateto), datecreated : xmlContent(datecreated), datelastupdated : xmlContent(datelastupdated), person : [{ ...self.getPersonAttributes(peopleData, "me"), reservationextid : xmlContent(order.id), firstname : order.customer && xmlContent(order.customer.first_name), lastname : order.customer && xmlContent(order.customer.last_name), address1 : order.shipping_address && xmlContent(order.shipping_address.address1), address2 : order.shipping_address && xmlContent(order.shipping_address.address2), zip : order.shipping_address && xmlContent(order.shipping_address.zip), city : order.shipping_address && xmlContent(order.shipping_address.city), countrycode : order.shipping_address && xmlContent(order.shipping_address.country_code), regioncode : order.shipping_address && xmlContent(order.shipping_address.province_code), phone : order.shipping_address && xmlContent(order.shipping_address.phone), email : xmlContent(order.email), mainperson : xmlContent("yes"), article : peopleItems["me"].map((line_item) => { return self.populateWintersteigerReservationArticle(line_item); }) }] } for (var personId in peopleItems) { if(personId == "me") continue; if (peopleItems.hasOwnProperty(personId)) { const personData = { ...self.getPersonAttributes(peopleData, personId), article : [] }; const items = peopleItems[personId]; personData.article = items.map((line_item) => { return self.populateWintersteigerReservationArticle(line_item); }); reservation.person.push(personData); } } return reservation } filterRentalItems(line_item){ return true; } getProperty(name, array){ const attribute = array.filter((property)=>{ return name == property.name; }); return attribute.length ? attribute[0].value : ""; } getMetafeild(namespace, key, array){ const feild = array.filter((feild)=>{ return key == feild.key && namespace == feild.namespace; }); return feild.length ? feild[0].value : ""; } collatePeopleItems(rentalItems) { const peopleItems = {}; for(var i = 0; i < rentalItems.length; i++){ const line_item = rentalItems[i]; let person = this.getProperty("person", line_item.properties); person = person || "unassigned" peopleItems[person] = peopleItems[person] || []; peopleItems[person].push(line_item); } return peopleItems; } populateWintersteigerReservationArticle(line_item){ const property1 = this.getProperty("property1", line_item.properties); const modelname = this.getMetafeild("rental", "model", line_item.metafields); const rentalgroupname = this.getMetafeild("rental", "group", line_item.metafields); const article = { rentalgroupextid : xmlContent(line_item.product_id), rentalgroupname : xmlContent(rentalgroupname), property1 : property1 && xmlContent(property1), modelname : modelname && xmlContent(modelname), quantity : xmlContent(line_item.quantity), // indicating that this item is taxable or that the totals in this item inlcude tax grossprice : xmlContent(line_item.price), discountpercent : xmlContent((line_item.total_discount / line_item.price) * 100), netprice : xmlContent(line_item.price - line_item.total_discount) // precentage of total tax or tax rate? }; return article; } getPeopleData(order){ const peopleString = this.getProperty("people", order.note_attributes); return peopleString ? JSON.parse(peopleString) : []; } getPersonAttributes(people, id){ let person: any = {}; for(var i =0; i < people.length; i++){ if(people[i].id == id){ person = { weightlbs: xmlContent(people[i].weight), heightinch: xmlContent(people[i].height), firstname: xmlContent(people[i].firstName), lastname: xmlContent(people[i].lastName) } if(people[i].gender == "Male"){ person.er_genderid = xmlContent(2); }else if(people[i].gender == "Female"){ person.er_genderid = xmlContent(1); } } } return person; } } // // // // IAT23897 // RX1000064 // www.somesite.com/voucherxxxxxx // 7 // HX13 // TACxxxx // 2011-11-10 // 2011-12-19 16:30:00 // 2011-12-20 // 2011-12-24 // 2011-12-24 // 2011-12-19T16:30:00 // no // // // // RX1000064 // IAT23897 // www.somesite.com/voucherxxxxxx // 17 // L203344 // 1 // Susan // Test // Street No 3 // Street No 3 // 3344 // SomeCity // SomeCity // CH // +33 7755 // +33 7756 // susan.test@test.com // // Miramar // 1994-02-23 // 23 // 11 // 80 // // 4 // 180 // // 1 // 20 // 333 // goofy // 53 // 1 // yes // 0.00 // yes // Remark //
// 10 // 10 // // // Ski *** // 170 // 38 // Fischer Progressor 900 // false // boot // false // // // 1 // 110.00 // 10 // 101.00 // 0 // // //
//
//
//