                                            metaOnly: false,
                                            objectType: MSObjectType.purchaseorder,
                                            collectionName: "purchaseOrders")
-        dict["demands"] = serialize(entities: demands.flatMap { $0 as? MSDocument }.map { MSEntity<MSDocument>.entity($0) },
+        dict["demands"] = serialize(entities: demands.compactMap { $0 as? MSDocument }.map { MSEntity<MSDocument>.entity($0) },
                                     parent: self,
                                     metaOnly: true,
                                     objectType: MSObjectType.demand,

                                    collectionName: "returns")
        dict["operations"] = serialize(entities: operations,
                                       parent: self,
                                       objectType: MSObjectType.customerorder, collectionName: "operations")
        dict["customerOrders"] = serialize(entities: customerOrders,
                                           parent: self,
                                           objectType: MSObjectType.customerorder, collectionName: "customerOrders")

}

extension MSDocument {
    public func metaAndLinkedSumDictionary() -> [String : Any] {
        var dict = dictionary(metaOnly: true)
        dict["linkedSum"] = linkedSum.minorUnits
        return dict
    }
    
    public func dictionary(metaOnly: Bool = true) -> Dictionary<String, Any> {
        var dict = [String: Any]()
        dict["meta"] = meta.dictionary()
        
        guard !metaOnly else { return dict }
        
        dict.merge(info.dictionary())
        dict.merge(id.dictionary())
        
        dict["linkedSum"] = linkedSum.minorUnits
        dict["sum"] = sum.minorUnits
        dict["agent"] = serialize(entity: agent, metaOnly: true)
        dict["contract"] = serialize(entity: contract, metaOnly: true)
